【问题标题】:Populating a Plotly graph from a two lists从两个列表中填充 Plotly 图
【发布时间】:2017-09-28 06:11:24
【问题描述】:

我正在尝试从 python 脚本中的两个列表创建一个简单的条形图。但是,当数据填充到绘图系统中时,它不会显示任何图表。下面是我编写的代码、列表的内容和 Plotly 生成的代码。 也许这是在填充图表之前需要以某种方式拆分列表的情况?原谅我这是一个新手问题,我以前从未尝试过。任何想法将不胜感激。

脚本代码:

 mycount = Counter()
 mytime = []
 with open('temp_dates.csv') as csvfile2:
     readCSV2 = csv.reader(csvfile2, delimiter=',')
     incoming = []
     for row in readCSV2:
          readin = row[0]
          time = row[1]
          year, month, day = (int(x) for x in readin.split('-'))
          ans = datetime.date(year, month, day)
          wkday = ans.strftime("%A")
          incoming.append([wkday,time])
          mycount[wkday] += 1
          mytime.append(time)
     with open('new_dates2.csv', 'w') as out_file:
         writer = csv.writer(out_file)
         writer.writerows(incoming)
 csvfile2.close()

 os.remove('./temp_dates.csv')

 daysum = []
 daylist = []
 for key,value in sorted(mycount.iteritems()):
     mylist = key, value
     daysum.append(value)
     daylist.append(key)

 s = pd.to_datetime(mytime).to_series()
 timesum = s.groupby(pd.cut(s.dt.hour, bins=np.arange(26, step=2),                include_lowest=True)).size()
 print(timesum)

 print(daylist,daysum)

 data = [go.Bar(
             x=[(daylist)],
             y=[(daysum)]
     )]
 py.plot(data, filename='basic-bar'

列表内容:

[0, 2]       2
(2, 4]       0
(4, 6]       9
(6, 8]      12
(8, 10]     12
(10, 12]     6
(12, 14]    15
(14, 16]     8
(16, 18]     4
(18, 20]     4
(20, 22]     3
(22, 24]     2

Plotly 创建的代码:

import plotly.plotly as py
from plotly.graph_objs import *
py.sign_in('username', 'api_key')
trace1 = {
  "x": [
    ["Friday", "Monday", "Saturday", "Sunday", "Thursday", "Tuesday",     "Wednesday], 
  "y": [
    [13, 11, 7, 9, 12, 13, 1], 
  "type": "bar", 
  "xsrc":     "quantumspores:1:b909c3,e16d42,692b77,e11300,9392dd,055dcf,274160", 
  "ysrc":     "quantumspores:1:bfbdce,f9103e,b0f1e0,d226d3,0091a5,40646f,5d2d35"
}
data = Data([trace1])
fig = Figure(data=data)
plot_url = py.plot(fig)

【问题讨论】:

    标签: python plotly


    【解决方案1】:

    你想绘制每天的条形图吗?

     data = [go.Bar(
                 x=[str(x) for x in daylist],
                 y=[int(x) for x in daysum]
         )]
    

    或者绘制每两天的条形图?

     data = [go.Bar(
                 x=[str(x) for x in timesum.index],
                 y=[int(x) for x in timesum]
         )]
    

    无论你选择什么,go.Bar 都需要 x 作为字符串,y 作为数字。

    希望对你有帮助。

    【讨论】:

    • 感谢您的评论。这适用于我需要制作的第二张图。 Timesum 将按一天中的时间显示请求数(以 2 小时为增量)。我要制作的第一个图表是按天显示的请求,它需要 daylist(星期几)和 daysum(每天的请求数)。
    • 刚看到你的第二个帖子 - 让我试一试!
    猜你喜欢
    • 2013-07-27
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多