从两个列表填充Ploly图

Populating a Plotly graph from a two lists(从两个列表填充Ploly图)

本文介绍了从两个列表填充Ploly图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

脚本代码:

 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

代码创建者:

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)

推荐答案

是否要绘制每天的条形图?

 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作为数字。

希望这能帮助您。

这篇关于从两个列表填充Ploly图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从两个列表填充Ploly图

基础教程推荐