[英]create a histogram with plotly.graph_objs like in plotly.express
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.我正在做可视化,我可以在 plotly express 中创建我想要的东西,但我必须多次使用不同的功能,所以我更喜欢使用 graph_objs 来制作子图,但我不知道如何创建它们。
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))
但是当我尝试在图形中进行操作时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 .由于没有数据展示,我根据官方参考中的例子用graph_objects创建了一个直方图。 instead of specifying a categorical variable as in express, we will deal with it by extracting the categorical variable.我们将通过提取分类变量来处理它,而不是像在 express 中那样指定分类变量。
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 ploty.express 版本
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.