You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Plotly Express冰柱图从水平(从右到左)改为垂直(从上到下)方向?

解决Plotly冰柱图垂直方向显示问题

直接在px.icicle()中添加orientation='v'参数就能实现从上到下的垂直布局,无需改用graph_objects。修改后的完整代码如下:

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
    'YEAR': ['2023', '2023', '2023', '2023'],
    'COMPANY': ['Company A','Company A','Company A','Company A'],
    'ORG': ['People', 'People', 'Customer', 'Customer'],
    'DEPARTMENT': ['HR', 'HR', 'Marketing', 'Marketing'],
    'AREA': ['Reward', 'Benefits', 'Campaigns', 'Insights'],
    'Employees': [5, 4, 9, 11],
    'Salary Cost' : [80000,75000,113000,140000]
})

# 添加orientation='v'设置垂直布局
figs = {
    c: px.icicle(df, path=["COMPANY","ORG", "DEPARTMENT", "AREA"], 
                 values=c, color='AREA', height=700, orientation='v')
    for c in ["Employees", "Salary Cost"]
}

fig = figs["Employees"]

# 按钮切换逻辑保持不变
fig.update_layout(
    updatemenus=[
        {
            "buttons": [
                {
                    "label": c,
                    "method": "restyle",
                    "args": [
                        {
                            "values": [figs[c].data[0].values],
                            "hovertemplate": figs[c].data[0].hovertemplate,
                        }
                    ],
                }
                for c in figs.keys()
            ]
        }
    ]
)

fig.show()

关键说明

  • orientation='v'是Plotly Express冰柱图的内置参数,直接控制布局方向:'h'为默认的水平(从右到左)布局,'v'则对应垂直(从上到下)布局。
  • 原有按钮切换逻辑无需改动,因为每个预生成的图表实例已经设置了垂直布局,切换时会自动保持该方向。

内容的提问来源于stack exchange,提问作者MaivishM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 07:20:20