从计数器字典中解析 Python 3.4 Pandas 和 Bokeh 中的年份
Parse Years in Python 3.4 Pandas and Bokeh from counter dictionary
我正在努力根据 collections
的 counter
函数的输出创建散景时间序列图。
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 轴。我尝试遵循 Time series tutorial,但我无法使用 pd.read_csv
和 parse_dates=['Date']
功能,因为我没有读取 csv 文件。
简单的方法是将数据转换为 pandas DataFrame(使用 pd.DataFrame),然后用年份列创建日期时间列。
简单示例:
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)
我正在努力根据 collections
的 counter
函数的输出创建散景时间序列图。
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 轴。我尝试遵循 Time series tutorial,但我无法使用 pd.read_csv
和 parse_dates=['Date']
功能,因为我没有读取 csv 文件。
简单的方法是将数据转换为 pandas DataFrame(使用 pd.DataFrame),然后用年份列创建日期时间列。
简单示例:
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)