【问题标题】:Displaying numbers on horizontal bar chart在水平条形图上显示数字
【发布时间】:2019-09-29 03:29:48
【问题描述】:

我正在尝试在堆叠的水平条形图上显示数字,并且我已经弄清楚如何使数字显示。但是,因为我有一些单数和双数,所以数字的方向有点奇怪。一位数的显示方式与两位数不同。有没有人建议我如何更改方向以使水平堆叠条显示水平方向的数字?

我已经尝试在 plotly 上做 textposition = "auto"。

fig = go.Figure()
fig.add_trace(go.Bar(x = publicstperc, 
                     y = publicstate_df.index,
                    orientation = "h", name = "Public",
                    marker_color = "rgb(41,84,140)",
                    text = publicstate_df["Number of Public Schools"],
                    textposition = "auto"))
fig.add_trace(go.Bar(x = privatenpperc,
                     y = privatenpstate_df.index,
                     orientation = "h", name = "Private-Nonprofit",
                    marker_color = "rgb(86,149,232)",
                    text = privatenpstate_df["Number of Private-Nonprofit Schools"],
                    textposition = "auto"))
fig.add_trace(go.Bar(x = proprietaryperc,
                     y = proprietarystate_df.index,
                    orientation = "h", name = "Proprietary",
                    marker_color = "rgb(179,232,255)",
                    text = proprietarystate_df["Number of Proprietary Schools"],
                    textposition = "auto"))
fig.update_xaxes(range=[0,100])
fig.update_layout(title_text = "School Types Participation by State",
                  barmode='stack',
                 height = 1250,
                 width = 1000)
fig.show()

我希望数字垂直显示,因为它是一个水平堆叠图表,但我发现单个数字显示水平。This is what my plot looks like:

【问题讨论】:

  • 请包括数据样本和所有import 行,以便重现性。确保发布的数据和可运行的代码可以从空的 Python 环境中生成您不想要的图。
  • @Ellaine Taz Ying Ho,听从 Parfait 的建议将大大增加您获得有用答案的机会。

标签: python plotly


【解决方案1】:

您可以在 go.Bar(...) 调用中添加 textangle=0 以强制文本在所有情况下都是水平的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 2015-12-25
    相关资源
    最近更新 更多