散景逐年折线图过程 [英] bokeh year on year line graph procedure

查看:58
本文介绍了散景逐年折线图过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

what is the best way for doing year over year line charts with daily data in bokeh?

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

currently im adding a dateline (arbitrarily for 2016) and year column to inital dataframe of daily values. Then pivoting to wide data by year filling in NAs (missing data varies across years) and then building bokeh graph line by line across the year cols:

说我有一张三年数据表:

Say I have a table of three years data:

列:日期和值

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')

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

Question i have is can this be further optimized? I would like to use hover in the future so what would be the best set up? Next step would be loops over years column but do I need to go that route?

来自R,我想将数据保留为长格式并执行以下操作:

Coming from R I would like to keep data in long format and do something like:

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

感谢您的提示.

推荐答案

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

One solution is to take your dates and create a year column and a day of year column using the .dt accessors

确保df ['date']是日期时间列.

Be sure that df['date'] is a datetime column.

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)

这篇关于散景逐年折线图过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆