Python 3.x 从计数器字典解析Python 3.4 Pandas和Bokeh中的年份
我正在努力从Python 3.x 从计数器字典解析Python 3.4 Pandas和Bokeh中的年份,python-3.x,pandas,bokeh,Python 3.x,Pandas,Bokeh,我正在努力从集合中的计数器函数的输出创建一个博克时间序列图 import pandas as pd from bokeh.plotting import figure, output_file, show import collections plotyears = [] counter = collections.Counter(plotyears) output_file("years.html") p = figure(width=800, height=250, x_axis_type
集合
中的计数器
函数的输出创建一个博克时间序列图
import pandas as pd
from bokeh.plotting import figure, output_file, show
import collections
plotyears = []
counter = collections.Counter(plotyears)
output_file("years.html")
p = figure(width=800, height=250, x_axis_type="datetime")
for number in sorted(counter):
yearvalue = number, counter[number]
p.line(yearvalue, color='navy', alpha=0.5)
show(p)
打印时,yearvalue
的输出为:
(2013, 132)
(2014, 188)
(2015, 233)
我怎样才能使博克将年份作为x轴,数字作为y轴。我已经试着遵循,但是我不能使用pd.read\u csv和p
arse\u dates=['Date']
功能,因为我没有读取csv文件 最简单的方法是将数据转换为一个pandas数据框(使用pd.DataFrame),然后使用year列创建一个datetime列
简单的例子:
import pandas as pd
from bokeh.plotting import figure, output_notebook, show
output_notebook()
years = [2012,2013,2014,2015]
val = [230,120,200,340]
# Convert your data into a panda DataFrame format
data=pd.DataFrame({'year':years, 'value':val})
# Create a new column (yearDate) equal to the year Column but with a datetime format
data['yearDate']=pd.to_datetime(data['year'],format='%Y')
# Create a line graph with datetime x axis and use datetime column(yearDate) for this axis
p = figure(width=800, height=250, x_axis_type="datetime")
p.line(x=data['yearDate'],y=data['value'])
show(p)
伟大的这完全奏效了。谢谢!我做了一点修改,让bokeh打印一个文件。如下所示:
import pandas as pd from bokeh.plotting import figure,output_file,show import collections
然后添加:output_file(“years.html”)