2

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()

enter image description here

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

1 Answer 1

4

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()

enter image description here

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()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Hey, Thank you for reply, I'm still trying to understand well, I add the table, I need to create it with only 2 features, Can you help me do it with 2 features because in your example you use "total_bill" and giving me a different result, thank you!
If my answer helped you, please consider accepting it as the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.