在 Plotly Express 中如何找到分组条形图上的 x 坐标?

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

How to find the x coordinate on a grouped bar chart on plotly express?

问题

以下是翻译好的部分:

  1. 跟随这里的回答 https://stackoverflow.com/questions/71610356/how-to-specify-the-x-coordinate-on-a-grouped-bar-chart-on-plotly
  2. 我想要以编程方式获取所有的x坐标。
  3. 我可以通过调整硬编码的数字来获得工作解决方案,例如:
  4. import plotly.express as px
  5. import plotly.graph_objects as go
  6. import numpy as np
  7. df = px.data.tips()
  8. print(df.head())
  9. fig = px.histogram(
  10. df, x="sex", y="total_bill", color="time", barmode="group", height=400
  11. )
  12. # 添加覆盖分类x轴的次要x轴
  13. fig.layout.xaxis2 = go.layout.XAxis(
  14. overlaying='x', range=[0, 2], showticklabels=False)
  15. # 添加线条跟踪,并与第二个x轴关联
  16. bargap = 0.2
  17. for i in range(2):
  18. x = bargap/2 + (1-bargap)/4
  19. y = [1111]
  20. print(111,x, y)
  21. scatt = fig.add_scatter(x=[i+x], y=y, xaxis='x2',
  22. showlegend=False, line={'color': 'gray'})
  23. x *=2.3
  24. print(22222,x, y)
  25. scatt = fig.add_scatter(x=[i+x], y=y, xaxis='x2',
  26. showlegend=False, line={'color': 'gray'})
  27. fig
  28. 但我需要一个动态解决方案,当我将颜色更改为“day”时也能正常工作:
  29. fig = px.histogram(
  30. df, x="sex", y="total_bill", color="day", barmode="group", height=400
  31. )
  32. 然后在子图中有4个框。在我处理的数据中,总是有不同数量的图和子图。
  33. 我真的希望plotly的开发人员能够帮助解决这个问题,这是我在使用plotly时遇到的第一个主要限制。
英文:

Following on from the response here https://stackoverflow.com/questions/71610356/how-to-specify-the-x-coordinate-on-a-grouped-bar-chart-on-plotly

I would like to get ALL the x coords, programatically.

I can get a working solution by playing around with hard coded numbers like:

  1. import plotly.express as px
  2. import plotly.graph_objects as go
  3. import numpy as np
  4. df = px.data.tips()
  5. print(df.head())
  6. fig = px.histogram(
  7. df, x="sex", y="total_bill", color="time", barmode="group", height=400
  8. )
  9. # Add secondary x-axis that overlays categorical xaxis
  10. fig.layout.xaxis2 = go.layout.XAxis(
  11. overlaying='x', range=[0, 2], showticklabels=False)
  12. # Add a line traces, and associate with the second xaxis
  13. bargap = 0.2
  14. for i in range(2):
  15. x = bargap/2 + (1-bargap)/4
  16. y = [1111]
  17. print(111,x, y)
  18. scatt = fig.add_scatter(x=[i+x], y=y, xaxis='x2',
  19. showlegend=False, line={'color': 'gray'})
  20. x *=2.3
  21. print(22222,x, y)
  22. scatt = fig.add_scatter(x=[i+x], y=y, xaxis='x2',
  23. showlegend=False, line={'color': 'gray'})
  24. fig

But I need a dynamic solution that works when I change color to day:

  1. fig = px.histogram(
  2. df, x="sex", y="total_bill", color="day", barmode="group", height=400
  3. )

and then have 4 boxes in the sub plot. In the data I'm working with there's always a different number of plots and sub plots.

I really hope the devs from plotly can help with this, it's the first major limitation I have come up against using plotly.

答案1

得分: 1

  1. import plotly.express as px
  2. df = px.data.tips()
  3. # Use primary and secondary in your function to vary
  4. primary = 'day'
  5. secondary = 'sex'
  6. fig = px.histogram(df, x=primary, y="total_bill", color=secondary, barmode="group", height=400)
  7. nPri = len(df[primary].unique())
  8. nSec = len(df[secondary].unique())
  9. N = nPri
  10. # Group centers
  11. xrange = N-1 # the underlying x-axis is scaled with N automatically
  12. xGp = [xrange * n / (N-1) for n in range(N)]
  13. # Plot group centers
  14. for ix in xGp:
  15. fig.add_vline(x=ix)
  16. bargap = 0.2 # I tried it with some variation. Still works.
  17. fig.layout.bargap = bargap
  18. # Calculate bar sizing
  19. gpWidth = 1 - bargap
  20. barWidth = gpWidth / nSec
  21. # Left and right edges of each group
  22. gpEdges = [(x - gpWidth / 2, x + gpWidth / 2) for x in xGp]
  23. for ix1, ix2 in gpEdges:
  24. gpRange = ix2 - ix1 # group range
  25. xB = [ix1 + barWidth/2 + barWidth * n for n in range(nSec)] # list of x coordinates of each bar in the group
  26. print(xB)
  27. for x in xB:
  28. fig.add_vline(x=x, line_color='blue')
  29. fig.show()
英文:

Found it.

  1. import plotly.express as px
  2. df = px.data.tips()
  3. # Use primary and secondary in your function to vary
  4. primary = 'day'
  5. secondary = 'sex'
  6. fig = px.histogram(df, x=primary, y="total_bill", color=secondary, barmode="group", height=400)
  7. nPri = len(df[primary].unique())
  8. nSec = len(df[secondary].unique())
  9. N = nPri
  10. # Group centers
  11. xrange = N-1 # the underlying x-axis is scaled with N automatically
  12. xGp = [xrange * n / (N-1) for n in range(N)]
  13. # Plot group centers
  14. for ix in xGp:
  15. fig.add_vline(x=ix)
  16. bargap = 0.2 # I tried it with some variation. Still works.
  17. fig.layout.bargap = bargap
  18. # Calculate bar sizing
  19. gpWidth = 1 - bargap
  20. barWidth = gpWidth / nSec
  21. # Left and right edges of each group
  22. gpEdges = [(x - gpWidth / 2, x + gpWidth / 2) for x in xGp]
  23. for ix1, ix2 in gpEdges:
  24. gpRange = ix2 - ix1 # group range
  25. xB = [ix1 + barWidth/2 + barWidth * n for n in range(nSec)] # list of x coordinates of each bar in the group
  26. print(xB)
  27. for x in xB:
  28. fig.add_vline(x=x, line_color='blue')
  29. fig.show()

huangapple
  • 本文由 发表于 2023年6月29日 10:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577668.html
匿名

发表评论

匿名网友

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

确定