问题
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|..
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)
I have been trying to strip the year from the date and still plot it as a date. The first line below works, but because of the leap years the second line returns an error in the real data (day is out of range for month).
df_snw['Date1'] = df_snw['Date1'].map(lambda x: x.strftime('%m-%d'))
df_snw = pd.to_datetime(df_snw['Date1'], format='%m-%d')
回答1:
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)
来源:https://stackoverflow.com/questions/43712333/pandas-visualizing-changes-in-event-dates-for-multiple-years-using-bokeh-or-any