I have two groups of data I'm working with, which I'd like to show in a bar plot using plotly (example for data is shown below).
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
values1 = abs(np.random.normal(0.5, 0.3, 13)) # random data and names
values2 = abs(np.random.normal(0.5, 0.3, 13))
values3 = abs(np.random.standard_normal(13))
values4 = abs(np.random.standard_normal(13))
names = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13']
fig = go.Figure()
fig.add_trace(go.Bar(
x = names,
y = values1,
legendgroup="group",
legendgrouptitle_text="method one",
name="first"
))
fig.add_trace(go.Bar(
x=names,
y=values2,
legendgroup="group",
name="second"
))
fig.add_trace(go.Bar(
x=names,
y=values3,
legendgroup="group2",
legendgrouptitle_text="method two",
name="first"
))
fig.add_trace(go.Bar(
x=names,
y=values4,
legendgroup="group2",
name="second"
))
fig.update_layout(barmode='group')
fig.update_traces(texttemplate='%{y:.2}', textposition='inside')
fig.show()
This code produces the following graph:
I would like to add a space between the two methods for each name (adding space between method one and method two for the two values in each value).
I tried using offsetgroup
but doesn't seem to work. Any help on the matter would be appreciated.
CodePudding user response:
I think you should use a subplots: Check this link, and see the examples.
fig = go.Figure()
fig.add_trace(go.Bar(
x = names,
y = values1,
legendgroup="group",
legendgrouptitle_text="method one",
name="first"
))
fig.add_trace(go.Bar(
x=names,
y=values2,
legendgroup="group",
name="second"
))
# add bar plot(null data)
fig.add_trace(go.Bar(
x=names,
y=np.full((1,51),np.NaN),
showlegend=False,
))
fig.add_trace(go.Bar(
x=names,
y=values3,
legendgroup="group2",
legendgrouptitle_text="method two",
name="first"
))
fig.add_trace(go.Bar(
x=names,
y=values4,
legendgroup="group2",
name="second"
))
fig.update_layout(barmode='group')#, bargroupgap=0.2
fig.update_traces(texttemplate='%{y:.2}', textposition='inside')
fig.show()