本文介绍了 pandas :使用Bokeh或任何其他绘图库可视化多年事件日期中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个图,其中y轴是我拥有数据的季节性年数,x轴以月和日为单位.每个季节年度都有两个日期.
I want to create a plot where the y-axis is the number of seasonal years I have data for and the x-axis is in months and days. Each seasonal year will have two dates.
|1957|...
|1956| d1--------d2
|1955| d1---------d2
|1954| d1---------d2
|June01|...|Jan01...|Feb11|...|Feb23|...|Feb26|...|Mar20|...|Mar25|..
除了x轴涵盖了整个时间范围,而不仅仅是12个月之外,我几乎有了所需的图表.
I almost have the graph I want, except the x-axis covers the entire time span rather than just 12-months.
from bokeh.plotting import figure
p1 = figure(plot_width=1000, plot_height=300, x_axis_type="datetime")
p1.circle(merged.date1, merged.index, color = 'red', legend = 'Date1')
p1.circle(merged.date2, merged.index, color = 'green', legend = 'Date2')
show(p1)
推荐答案
我会将x轴上的date1和date2转换为day of the year
,然后将x刻度重新标记为月份.这样,所有数据都将覆盖在1到365个x轴刻度上.
I would convert your date1 and date2 to day of the year
for the xaxis and re-label the x ticks as the months. This way all the data is overlayed on a 1 to 365 xaxis scale.
df = pd.DataFrame({'date1':['1954-03-20','1955-02-23','1956-01-01','1956-11-21','1958-01-07'],
'date2':['1954-03-25','1955-02-26','1956-02-11','1956-11-30','1958-01-17']},
index=['1954','1955','1956','1957','1958'])
df['date2'] = pd.to_datetime(df['date2'])
df['date1'] = pd.to_datetime(df['date1'])
df=df.assign(date2_DOY=df.date2.dt.dayofyear)
df=df.assign(date1_DOY=df.date1.dt.dayofyear)
from bokeh.plotting import figure, show
from bokeh.io import output_notebook
from bokeh.models import FuncTickFormatter, FixedTicker
p1 = figure(plot_width=1000, plot_height=300)
p1.circle(df.date1_DOY,df.index, color='red', legend='Date1')
p1.circle(df.date2_DOY,df.index, color='green', legend='Date2')
p1.xaxis[0].ticker=FixedTicker(ticks=[1,32,60,91,121,152,182,213,244,274,305,335,366])
p1.xaxis.formatter = FuncTickFormatter(code="""
var labels = {'1':'Jan',32:'Feb',60:'Mar',91:'Apr',121:'May',152:'Jun',182:'Jul',213:'Aug',244:'Sep',274:'Oct',305:'Nov',335:'Dec',366:'Jan'}
return labels[tick];
""")
show(p1)
这篇关于 pandas :使用Bokeh或任何其他绘图库可视化多年事件日期中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!