散景逐年折线图程序
bokeh year on year line graph procedure
用散景中的每日数据制作年复一年折线图的最佳方法是什么?
目前我正在向每日值的初始数据框添加一个日期线(任意为 2016 年)和年份列。然后按年填充 NA 转向宽数据(缺失数据因年份而异),然后在一年中逐行构建散景图 cols:
假设我有一个 table 三年的数据:
列:日期和值
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')
我的问题是这可以进一步优化吗?我想在将来使用悬停,那么最好的设置是什么?下一步将是循环年专栏,但我需要走那条路吗?
来自 R 我想以长格式保存数据并执行以下操作:
p.line(df, x='dateline' , y = 'value' , color = 'year')
感谢您的提示。
一个解决方案是获取日期并使用 .dt 访问器创建年份列和年份列
确保 df['date'] 是日期时间列。
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)
用散景中的每日数据制作年复一年折线图的最佳方法是什么?
目前我正在向每日值的初始数据框添加一个日期线(任意为 2016 年)和年份列。然后按年填充 NA 转向宽数据(缺失数据因年份而异),然后在一年中逐行构建散景图 cols:
假设我有一个 table 三年的数据:
列:日期和值
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')
我的问题是这可以进一步优化吗?我想在将来使用悬停,那么最好的设置是什么?下一步将是循环年专栏,但我需要走那条路吗?
来自 R 我想以长格式保存数据并执行以下操作:
p.line(df, x='dateline' , y = 'value' , color = 'year')
感谢您的提示。
一个解决方案是获取日期并使用 .dt 访问器创建年份列和年份列
确保 df['date'] 是日期时间列。
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)