Reputation: 115
I am trying to plot data with datetime as x-axis. The data is collected over several years. I can convert the Date to datetime format, extract the year portion of the datetime and plot using that as color.
df['Date']=pd.to_datetime(df['Date'],errors='coerce')
df['year']=df['Date'].dt.year
df['mthday']=df['Date'].dt.strftime('%d-%b')
df=df.sort_values(by='Date')
plot=px.line(df,x='Date',y='value',color='year')
(I have tried the solution at this link: Plotly: How to plot just month and day on x axis? (Ignore year))
This gives me the graph below. The different years are correctly shown in different colors. However, the x-axis spans from 2014 to 2020. I want the x-axis to only show Jan to Dec and the graphs for different years to be superposed on this. (aka: shortening/ collapsing the x-axis to only one set of months from Jan to Dec)
Edited to add sample from df:
Date/ value/value2/value3/Note/Unnamed/year/mthday
187 2015-11-10 125 73 79.0 NaN NaN 2015 10-Nov
108 2015-01-25 132 88 61.0 NaN NaN 2015 25-Jan
85 2014-12-25 138 86 69.0 NaN NaN 2014 25-Dec
154 2015-07-24 131 80 62.0 NaN NaN 2015 24-Jul
226 2016-09-18 120 73 61.0 NaN NaN 2016 18-Sep
261 2018-06-07 135 72 64.0 NaN NaN 2018 07-Jun
59 2014-11-20 158 79 70.0 NaN NaN 2014 20-Nov
118 2015-02-12 145 77 70.0 NaN NaN 2015 12-Feb
287 2019-09-02 130 79 66.0 NaN NaN 2019 02-Sep
228 2016-09-26 104 76 59.0 NaN NaN 2016 26-Sep
(Actually for the graph, I would want to plot value,value2,value3 for all the years. In this example, I have only tried for value for all the years)
2nd edit for possible answer: Hi, I think i have found a way to do this. I use the mthday column and convert it back to datetime. It defaults the year value to 1900. I then plot this using mthday as x-axis and year as color. Then hide the year value in the x-axis tickmark.
df['mthday']=pd.to_datetime(df['mthday'],format='%d-%b',errors='coerce')
fig=px.line(df,x='mthday',y='value',color='year')
fig.update_layout(xaxis_tickformat='%d-%b')
Let me know if you have a better way to accomplish this.
Upvotes: 5
Views: 1803
Reputation: 115
Posting below as one way that I have found to do this: I use the mthday column and convert it back to datetime type. This puts all the year values for all the rows to default value of 1900. I then use mthday as x-axis and hide the year by specifying the x-axis tickmarks.
df['mthday']=df['Date'].dt.strftime('%d-%b')
df['mthday']=pd.to_datetime(df['mthday'],format='%d-%b',errors='coerce')
df=df.sort_values(by='Date')
plot=px.line(df,x='mthday',y='value',color='year')
plot.update_layout(xaxis_tickformat='%d-%b')
Upvotes: 3