从 Plotly 图表的 X 轴标签中移除数据

huangapple go评论69阅读模式
英文:

Remove Data from X Axis Labels plotly

问题

    标签 = ["示例A", "示例B", "示例C", "示例D", "示例E", "示例F", "示例G", "示例H", "示例I", "示例J", "示例K", "示例L", "示例M", "示例N", "示例O", "示例P", "示例Q", "示例R", "示例S", "示例T"]
    宽度 = np.array([7000, 5000, 4000, 4000, 3000, 2000, 2000, 2000, 2000, 1800, 1700, 1500, 1300, 1200, 1200, 1100, 1000, 1000, 1000, 1000])
    数据 = {"5": [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
          "4": [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
          "3": [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
          "2": [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20],
          "1": [20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20]}
    图表 = go.Figure()
    forin 数据:
        图表.add_trace(go.Bar(
            name=键,
            y=数据[键],
            x=np.cumsum(宽度) - 宽度,
            width=宽度,
            customdata=np.transpose([标签]),
            textposition="inside",
            insidetextanchor="middle",
        ))
    图表.update_xaxes(
        tickvals=np.cumsum(宽度) - 宽度 / 2,
        ticktext=["%s<br>%d" % (l, w) for l, w in zip(标签, 宽度)]
    )
    图表.update_layout(
        barmode="stack",
        uniformtext=dict(mode="hide", minsize=18),
        height=1000,
        width=2000,
    )
英文:

从 Plotly 图表的 X 轴标签中移除数据Is there a way to remove the x axis data from the x axis? I need the labels, just not the data/volume. (i.e display "Example A", not "Example A 7000")

labels = [&quot;Example A&quot;,&quot;Example B&quot;,&quot;Example C&quot;,&quot;Example D&quot;,&quot;Example E&quot;,&quot;Example F&quot;,&quot;Example G&quot;,&quot;Example H&quot;,&quot;Example I&quot;,&quot;Example J&quot;,&quot;Example K&quot;,&quot;Example L&quot;,&quot;Example M&quot;,&quot;Example N&quot;,&quot;Example O&quot;,&quot;Example P&quot;,&quot;Example Q&quot;,&quot;Example R&quot;,&quot;Example S&quot;,&quot;Example T&quot;,]
widths = np.array([7000,5000,4000,4000,3000,2000,2000,2000,2000,1800,1700,1500,1300,1200,1200,1100,1000,1000,1000,1000,])
data = { &quot;5&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],
&quot;4&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],
&quot;3&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],
&quot;2&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],
&quot;1&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],}
fig = go.Figure()
for key in data:
fig.add_trace(go.Bar(
name=key,
y=data[key],
x=np.cumsum(widths)-widths,
width=widths,
customdata=np.transpose([labels]),
textposition=&quot;inside&quot;,
insidetextanchor = &quot;middle&quot;,
fig.update_xaxes(
tickvals=np.cumsum(widths)-widths/2,
ticktext= [&quot;%s&lt;br&gt;%d&quot; % (l, w) for l, w in zip(labels, widths)])
fig.update_layout(
barmode=&quot;stack&quot;,
uniformtext=dict(mode=&quot;hide&quot;, minsize=18),
height = 1000,
width = 2000,)

答案1

得分: 1

你明确地在这一行中将宽度包含为标签:

["%s<br>%d" % (l, w) for l, w in zip(labels, widths)]


将其更改为:

["%s" % l for l in labels]


然后它应该可以工作
英文:

You're explicitly including the widths as labels in this line:

[&quot;%s&lt;br&gt;%d&quot; % (l, w) for l, w in zip(labels, widths)]

Change that to:

[&quot;%s&quot; % l for l in labels]

And it should work

huangapple
  • 本文由 发表于 2023年2月24日 01:24:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定