Hi Mae.
I am not sure I completely understand what you want, but maybe this is it?
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
data = px.data.stocks()[['date', 'GOOG']]
data['year'] = pd.to_datetime(data['date']).dt.year.astype(str)
data['month'] = pd.to_datetime(data['date']).dt.month
data['ym'] = pd.to_datetime(data['date']).apply(lambda x: str(x.year)+'_'+f'{x.month:02}')
data.drop_duplicates(['year', 'month'], keep='last', inplace=True)
selected_years = ['2018', '2019']
fig3 = make_subplots(specs=[[{"secondary_y": True}]])
fig3.add_trace(
go.Bar(
x=data[data.year == selected_years[0]]['month'],
y=data[data.year == selected_years[0]]['GOOG'],
name=selected_years[0],
offsetgroup=1
),
secondary_y=False,
)
fig3.add_trace(
go.Bar(
x=data[data.year == selected_years[1]]['month'],
y=data[data.year == selected_years[1]]['GOOG'],
name=selected_years[1],
offsetgroup=2
),
secondary_y=True,
)
fig3.show()
If that’s the case, this post might be helpful too: Group bar chart with secondary y axis in plotly by python - #5 by jawabutt
The other option would be:
# OPTION 2
fig2 = px.bar(data_frame=data, x='ym', y='GOOG', color='year')
fig2.show()
I hope this helps!