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

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

Remove Data from X Axis Labels plotly

问题

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

从 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")

  1. 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;,]
  2. widths = np.array([7000,5000,4000,4000,3000,2000,2000,2000,2000,1800,1700,1500,1300,1200,1200,1100,1000,1000,1000,1000,])
  3. data = { &quot;5&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],
  4. &quot;4&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],
  5. &quot;3&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],
  6. &quot;2&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],
  7. &quot;1&quot;: [20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20],}
  8. fig = go.Figure()
  9. for key in data:
  10. fig.add_trace(go.Bar(
  11. name=key,
  12. y=data[key],
  13. x=np.cumsum(widths)-widths,
  14. width=widths,
  15. customdata=np.transpose([labels]),
  16. textposition=&quot;inside&quot;,
  17. insidetextanchor = &quot;middle&quot;,
  18. fig.update_xaxes(
  19. tickvals=np.cumsum(widths)-widths/2,
  20. ticktext= [&quot;%s&lt;br&gt;%d&quot; % (l, w) for l, w in zip(labels, widths)])
  21. fig.update_layout(
  22. barmode=&quot;stack&quot;,
  23. uniformtext=dict(mode=&quot;hide&quot;, minsize=18),
  24. height = 1000,
  25. width = 2000,)

答案1

得分: 1

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

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

  1. 将其更改为:

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

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

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

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

Change that to:

  1. [&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:

确定