繁体   English   中英

散景逐年折线图过程

[英]bokeh year on year line graph procedure

用散景中的每日数据绘制逐年折线图的最佳方法是什么?

目前,我在每日值的初始数据框中添加了日期线(2016年任意)和年份列。 然后通过按年度填充NA来转向广泛的数据(丢失的数据会因年份而异),然后逐年逐行逐行构建bokeh图:

假设我有一张三年数据表:

列:日期和值

df = df.set_index('Date')

df['dateline'] = df.index.to_series().dt.strftime('%d-%b-2016')
df['year'] = df.index.to_series().dt.strftime('%Y')

pv = pd.pivot_table(df, index=df['dateline'], columns=df.index.year,
                    values='value', aggfunc='sum')

pv.index = pd.to_datetime(pv.index, format = '%d-%b-%Y' )
pv.sort_index(inplace=True)
pv = pv.apply(lambda x: x.fillna(method = 'ffill' , limit = 4))


p.line(x= pv.index , y = pv[2017], line_width=1.5, line_color = "red" ,legend = '2017')
p.line(x= pv.index , y = pv[2016], line_width=1.5, line_color = "blue" ,legend = '2016')
p.line(x= pv.index , y = pv[2015], line_width=1.5, line_color = "green" , legend = '2015')
p.line(x= pv.index , y = pv[2014], line_width=1.5, line_color = "orange" ,legend = '2014')

我的问题是可以进一步优化吗? 我想将来使用悬停功能,那么最好的设置是什么? 下一步将是“多年循环”专栏,但我需要走那条路线吗?

来自RI的用户希望将数据保留为长格式,并执行以下操作:

p.line(df, x='dateline' , y = 'value' , color = 'year')

感谢您的提示。

一种解决方案是获取日期,并使用.dt访问器创建“年”列和“年中的某日”列

确保df ['date']是datetime列。

df['year'] = df['date'].dt.year
df['dayofyear'] = df['date'].dt.dayofyear

df.head()

            year     value  dayofyear
date                                 
2014-01-31  2014  1.964372         31
2014-02-28  2014  2.386228         59
2014-03-31  2014  2.695743         90
2014-04-30  2014  2.712133        120
2014-05-31  2014  2.033271        150


from bokeh.charts import Line
p = Line(df,x='dayofyear', y='value',color='year')
show(p)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM