使用 plotly.graph_objs 创建直方图,如 plotly.express
create a histogram with plotly.graph_objs like in plotly.express
我正在做可视化,我可以用 plotly express 创建我想要的东西,但我必须用不同的功能做很多次,所以我更喜欢使用 graph_objs 来制作子图,但我不知道如何创建它们。
fig = px.histogram(eda, x="HeartDisease", color="Sex", barmode="group", height=450, width = 450) fig.show()
但是当我尝试在图表中这样做时 fig.add_trace(go.Histogram( x = eda['HeartDisease'], name=eda.Sex))
错误:'name' 属性 是字符串,必须指定为:- 字符串 - 将转换为字符串的数字
fig.add_trace(go.Histogram( x = eda['HeartDisease'], color=eda.Sex))
错误:错误属性路径:颜色
希望你能帮帮我!
数据
Sex
HeartDisease
Male
HeartDisease
Female
Normal
Female
HeartDisease
Male
HeartDisease
Male
Normal
由于没有数据展示,我根据official reference中的例子制作了一个graph_objects的直方图。我们将通过提取分类变量来处理它,而不是像在 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版本
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()
我正在做可视化,我可以用 plotly express 创建我想要的东西,但我必须用不同的功能做很多次,所以我更喜欢使用 graph_objs 来制作子图,但我不知道如何创建它们。
fig = px.histogram(eda, x="HeartDisease", color="Sex", barmode="group", height=450, width = 450) fig.show()
但是当我尝试在图表中这样做时 fig.add_trace(go.Histogram( x = eda['HeartDisease'], name=eda.Sex))
错误:'name' 属性 是字符串,必须指定为:- 字符串 - 将转换为字符串的数字
fig.add_trace(go.Histogram( x = eda['HeartDisease'], color=eda.Sex))
错误:错误属性路径:颜色
希望你能帮帮我!
数据
Sex | HeartDisease |
---|---|
Male | HeartDisease |
Female | Normal |
Female | HeartDisease |
Male | HeartDisease |
Male | Normal |
由于没有数据展示,我根据official reference中的例子制作了一个graph_objects的直方图。我们将通过提取分类变量来处理它,而不是像在 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版本
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()