I am generating bar plots for different columns of the same data frame, i.e. the x axes is common to all plots.
Besides the size of the bars, also their color represents the value of each column.
I read here that plotly express figures cannot be added with add_traces
(which adds traces, not figures, as the name suggests). In the post linked above, it is suggested to pass the data
of the figure, but then I would lose the layout
of my figures, without knowing how to set it again in the subplots.
What solution is more practical to add figures to subplots without losing the layout?
I am new to plotly, so itβs likely that I am missing the logic behind it. If this is the case, please suggest training material and/or tutorials which may help me grasping it. Thank you!
min, max = 0, 1
size=5
data = {'categorical_var' : ['a', 'b', 'c', 'd', 'e'],
'num_var1' : [np.random.uniform(min, max) for _ in range(size)],
'num_var2' : [np.random.uniform(min, max) for _ in range(size)],
'num_var3' : [np.random.uniform(min, max) for _ in range(size)]}
df = pd.DataFrame.from_dict(data)
fig1 = px.bar(df, x='categorical_var', y='num_var1',
color='num_var1',
labels={'categorical_var':'Categories', 'num_var1':'Variable 1'},
width=900,
height =300
)
fig2 = px.bar(df, x='categorical_var', y='num_var2',
color='num_var2',
labels={'categorical_var':'Categories', 'num_var2':'Variable 2'},
width=900,
height =300
)
fig1 = px.bar(df, x='categorical_var', y='num_var3',
color='num_var3',
labels={'categorical_var':'Categories', 'num_var3':'Variable 3'},
width=900,
height =300
)
fig1.update_layout(
margin=dict(r=5, l=5),
coloraxis=dict(colorscale='Bluered_r'),
template='plotly_white'
)
fig2.update_layout(
margin=dict(r=5, l=5),
coloraxis=dict(colorscale='Bluered_r'),
template='plotly_white'
)
fig3.update_layout(
margin=dict(r=5, l=5),
coloraxis=dict(colorscale='Bluered_r'),
template='plotly_white'
)
fig1.show()
fig2.show()
fig3.show()