Pandas:使用 Bokeh 或任何其他绘图库可视化事件日期多年的变化

Pandas: Visualizing Changes in Event Dates for Multiple Years using Bokeh or any other plotting library

我想创建一个图,其中 y 轴是我有数据的季节性年份数,x 轴以月和天为单位。每个季节性年份都有两个日期。

|1957|...
|1956|             d1--------d2
|1955|                                d1---------d2 
|1954|                                                    d1---------d2
     |June01|...|Jan01...|Feb11|...|Feb23|...|Feb26|...|Mar20|...|Mar25|..

我几乎得到了我想要的图表,只是 x 轴涵盖了整个时间跨度,而不仅仅是 12 个月。

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)

我一直在尝试从日期中删除年份,但仍将其绘制为日期。下面的第一行有效,但由于闰年,第二行 returns 实际数据中存在错误(日期超出月份范围)。 df_snw['Date1'] = df_snw['Date1'].map(lambda x: x.strftime('%m-%d')) df_snw = pd.to_datetime(df_snw['Date1'], format='%m-%d')

我会将 x 轴的日期 1 和日期 2 转换为 day of the year,并将 x 刻度重新标记为月份。这样,所有数据都覆盖在 1 到 365 的 x 轴刻度上。

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)