如何使用Plotly在单个图中并行绘制两列数值的条形图?

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

How to plot bar graph with two column values parallely in a single figure using plotly?

问题

我有一个pandas数据框,显示在每个购物中心,男性花了多少钱,女性花了多少钱。就像这样 -

  1. ShoppingMall MaleSpends FemaleSpends
  2. 0 XX 5600.20 4500.70
  3. 1 YY 9000.00 100000.00
  4. 2 zz 7809.45 5600.89

在一个图表中,我需要绘制男性支出和女性支出。

我可以在Plotly中分别绘制这些图表 -

  1. import plotly.express as px
  2. fig1=px.bar(data,x='ShoppingMall',y='MaleSpends',barmode='group')
  3. fig2=px.bar(data,x='ShoppingMall',y='FemaleSpends',barmode='group')

但我需要在一个图中绘制男性和女性花费的金额。

一个截图如下所示:

如何使用Plotly在单个图中并行绘制两列数值的条形图?

英文:

I have a pandas dataframe which shows in each shopping mall, how much the males spend money and how much the females spend money. Its like this -

  1. ShoppingMall MaleSpends FemaleSpends
  2. 0 XX 5600.20 4500.70
  3. 1 YY 9000.00 100000.00
  4. 2 zz 7809.45 5600.89

In one graph I have to plot the malespends and femalespends.

I can plot the graphs separately in plotly -

  1. import plotly.express as px
  2. fig1=px.bar(data,x='ShoppingMall',y='MaleSpends',barmode='group')
  3. fig2=px.bar(data,x='ShoppingMall',y='FemaleSpends',barmode='group')

But I need to plot the money spent by males and females in one figure.
A screenshot is shown .如何使用Plotly在单个图中并行绘制两列数值的条形图?

答案1

得分: 0

  1. import plotly.graph_objects as go
  2. # 创建一个具有两个图迹的图形
  3. fig = go.Figure()
  4. # 用于MaleSpends的第一个图迹
  5. fig.add_trace(
  6. go.Bar(
  7. x=data['ShoppingMall'],
  8. y=data['MaleSpends'],
  9. name='男性支出'
  10. )
  11. )
  12. # 用于FemaleSpends的第二个图迹
  13. fig.add_trace(
  14. go.Bar(
  15. x=data['ShoppingMall'],
  16. y=data['FemaleSpends'],
  17. name='女性支出'
  18. )
  19. )
  20. # 更新布局以显示图例并堆叠条形图
  21. fig.update_layout(
  22. barmode='group',
  23. title_text='购物中心的性别支出',
  24. xaxis_title='购物中心',
  25. yaxis_title='支出',
  26. )
  27. # 显示图表
  28. fig.show()
英文:
  1. import plotly.graph_objects as go
  2. # Create a figure with two traces
  3. fig = go.Figure()
  4. # First trace for MaleSpends
  5. fig.add_trace(
  6. go.Bar(
  7. x=data['ShoppingMall'],
  8. y=data['MaleSpends'],
  9. name='Male Spends'
  10. )
  11. )
  12. # Second trace for FemaleSpends
  13. fig.add_trace(
  14. go.Bar(
  15. x=data['ShoppingMall'],
  16. y=data['FemaleSpends'],
  17. name='Female Spends'
  18. )
  19. )
  20. # Update layout to show the legend and to stack the bars
  21. fig.update_layout(
  22. barmode='group',
  23. title_text='Spending by Gender in Shopping Malls',
  24. xaxis_title='Shopping Mall',
  25. yaxis_title='Spends',
  26. )
  27. # Show the plot
  28. fig.show()

This will create a grouped bar plot where each mall has two bars side-by-side - one for MaleSpends and another for FemaleSpends. If you want a stacked bar plot where each mall has a single bar, with MaleSpends and FemaleSpends stacked on top of each other, just change barmode to 'stack' in the update_layout call.

答案2

得分: 0

在其最简单形式下,使用plotly的express函数创建代码,可以使用以下一行代码创建图形。

  1. px.bar(df, x='ShoppingMall', y=['MaleSpends', 'FemaleSpends'], barmode='group')

如何使用Plotly在单个图中并行绘制两列数值的条形图?

英文:

At its simplest, using plotly's expres function to create the code, the graph can be created with the following one line.

  1. px.bar(df, x='ShoppingMall', y=['MaleSpends', 'FemaleSpends'], barmode='group')

如何使用Plotly在单个图中并行绘制两列数值的条形图?

huangapple
  • 本文由 发表于 2023年6月8日 21:58:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76432633.html
匿名

发表评论

匿名网友

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

确定