Python Plotly Facet Plot Y 轴标题更改

Python Plotly Facet Plot Y-axis title change

提问人:Markus 提问时间:10/10/2023 最后编辑:Markus 更新时间:10/11/2023 访问量:70

问:

当我运行以下内容时:

df = px.data.tips()
fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group", facet_row="time", facet_col="day",
       category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
fig.update_layout(yaxis_title="Total Bill ($)")
fig.show()

我遇到的问题是,在我的绘图的 2 个 y 轴标题中,只有一个更改为“总账单($)”,另一个保持在“total_bill”。

(https://i.stack.imgur.com/oUu0J.png)

有人知道发生了什么吗?

python plotly yaxis

评论


答:

0赞 BigBen 10/10/2023 #1

该列有一个非情节选项:rename

fig = px.bar(df.rename(columns={"total_bill": "Total Bill ($)"}), 
             x="sex", y="Total Bill ($)", color="smoker", barmode="group", 
             facet_row="time", facet_col="day",
             category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], 
                              "time": ["Lunch", "Dinner"]})
fig.show()

输出:enter image description here

0赞 Markus 10/11/2023 #2

我想出了另一种方法,而无需更改数据列名称。在此特定图表中,两个 y 轴标题位于 yaxis1 和 yaxis5 下,因此代码需要为:

df = px.data.tips()
fig = px.bar(df, x="sex", y="total_bill", color="smoker", barmode="group", facet_row="time", facet_col="day",
       category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]})
fig.update_layout(
    yaxis=dict(
        title_text="Total Bill",
    ),    
    yaxis5=dict(
        title_text="Total Bill",
    ),  
    
)
fig.show()

我想如果子图的数量不同,那么 yaxis# 就会不同。可以打印(图)来弄清楚数字是什么。不是最聪明的方法,但它可以完成工作。

评论

1赞 russhoppa 10/11/2023
是的,当您在 Plotly 中制作子图时,有 n 个轴,可以通过这种方式访问。如果您愿意,您也可以进行设置。xyfig.layout.yaxis5.title = 'Total Bill'