I'm doing visualization and I can create what I want in plotly express but I have to do it many times with different features so I prefer to use graph_objs to make subplots but I don't know how to create them.
fig = px.histogram(eda, x="HeartDisease", color="Sex", barmode="group", height=450, width = 450) fig.show()
but when I try to do it in graph fig.add_trace(go.Histogram( x = eda['HeartDisease'], name=eda.Sex))
error: The 'name' property is a string and must be specified as: - A string - A number that will be converted to a string
fig.add_trace(go.Histogram( x = eda['HeartDisease'], color=eda.Sex))
error: Bad property path: color
I hope you can help me!
the data
Sex | HeartDisease |
---|---|
Male | HeartDisease |
Female | Normal |
Female | HeartDisease |
Male | HeartDisease |
Male | Normal |
Since there is no data presentation, I created a histogram with graph_objects based on the examples in the official reference. instead of specifying a categorical variable as in express, we will deal with it by extracting the categorical variable.
import plotly.graph_objects as go
df = px.data.tips()
fig = go.Figure()
fig.add_trace(go.Histogram(histfunc="count",
y=df.query('sex == "Female"')['total_bill'],
x=df.query('sex == "Female"')['day'],
name="Female")
)
fig.add_trace(go.Histogram(histfunc="count",
y=df.query('sex == "Male"')['total_bill'],
x=df.query('sex == "Male"')['day'],
name="Male")
)
fig.update_layout(xaxis_title='day', yaxis_title='Count', legend_title='sex')
fig.update_xaxes(categoryorder='array', categoryarray=["Thur", "Fri", "Sat", "Sun"])
fig.show()
ploty.express version
import plotly.express as px
df = px.data.tips()
fig = px.histogram(df, x="day", color='sex', barmode='group', category_orders=dict(day=["Thur", "Fri", "Sat", "Sun"]))
fig.show()