Hello,
I’m making a dashboard for environmental monitoring.
Essentially the task consists is:
(on the y axis) plotting the concentrations of a given pollutant and for (possibly multiple) monitoring stations,
(on the x axis) the date when this concentration occurred.
For some reason when plotting for more than one station the x-axis repeats itself leading to broken plot.
By repeat itslef I mean, once it reache the end of the month, add some other dates at the end.
As you can see from the image the 21 of February reappears.
This is the code that I use for generating the plot.
def plot_pollutant_multi_stations_date(start_date, end_date, pollutant, poll_df, stations,
limits_df, title):
poll_df = poll_df.sort_values(by='Date')
poll_df = poll_df[(poll_df['Date']>=start_date) & (poll_df['Date']<=end_date) &
(poll_df['Station'].isin(stations))]
#poll_df = poll_df[poll_df['Month'] == month]
poll_df = poll_df.sort_values(['To Date']).reset_index(drop=True)
#pdb.set_trace()
fig = px.line(poll_df, x = 'To Date', y = pollutant, color = 'Station')
#fig.layout.xaxis.tickmode = 'array'
#fig.layout.xaxis.ticktext = poll_df['Date']
fig.layout.xaxis.nticks = 10
x_0 = poll_df['To Date'].min()
x_f = poll_df['To Date'].max()
#add Limit
lim = limits_df[limits_df['Pollutant'] == pollutant]['Level']
fig.update_layout(title=title + ' for ' + pollutant)
#pdb.set_trace()
if not lim.empty:
lim = lim.to_numpy()[0]
fig.add_hline(y=lim, line_color='red', annotation_text='Limit')
return fig
This happens for datetimes when one of the stations doesn’t have a datatip.
Any ideas on how to handle this?
Thank you!