I am getting data in a pandas dataframe, there are four columns in the dataframe ‘‘year’, week_number’, ‘week_ending_date’ and ‘value’. Data is for several years (2012 - 2019).
Each year is represented as a line in the chart using Scatter with x = df[‘week’] and y = df[‘value’].
So far so good. I am able to view the values against week number for each year.
Instead of showing the week number as x axis ticks, I would like to show the months (Jan, Feb, Mar etc.) I tried to use tickvals, ticktext approach however since more than one week falls in a month that didn’t work well.
I have pasted the code below. Data is read from a database in get_data_for_series call and it just returns a dataframe with four columns as described in the first paragraph above.
A screen shot of sample data is below
One of the sub plots which is representative of others is below
def plot(series_names):
fig = make_subplots(
rows=3,
cols=2,
horizontal_spacing=0.1,
vertical_spacing=0.1,
subplot_titles=series_names
)
showlegend = True
for col, series_name in enumerate(series_names):
df_data = get_data_for_series(series_name)
year_list = df_data['year'].unique().tolist()
for idx, year in enumerate(year_list):
df = df_data[df_data['year'] == year][['week_ending_date','week', 'value']].reset_index()
df = df.sort_values(by=['week'])
trace = go.Scatter(x=df['week'],
y=df['value'],
line=dict(width=PLOT_COLORS_WIDTH[year][1], color=PLOT_COLORS_WIDTH[year][0]),
mode='lines',
name=year,
legendgroup=str(year),
showlegend=showlegend)
column_position = col % COL_COUNT
row_position = math.ceil((col + 1) / COL_COUNT)
fig.add_trace(trace, row=row_position, col=column_position + 1)
fig.update_layout(title_text='',
title_font_size=12,
autosize=False,
plot_bgcolor=BACKGROUND_COLOR,
width=WIDTH,
height=HEIGHT)
for i in range(1,7):
fig['layout'][f'xaxis{i}'].update(title='Week Number',
showgrid=True,
mirror=True,
linecolor='black',
gridcolor=GRID_COLOR)
for i in range(1,7):
fig['layout'][f'yaxis{i}'].update(title='bbls',
showgrid=True,
mirror=True,
linecolor='black',
gridcolor=GRID_COLOR,
tickformat='g')
showlegend = False
#Set Sub Plot title Font
for annotation in fig['layout']['annotations']:
annotation['font'] = dict(size=12)
print(fig['layout'])
fig.show()