绘图条形图带有多列图

发布于 2025-02-13 15:59:44 字数 789 浏览 3 评论 0原文

我有一个类似的数据集:

Female    Male    Single    Married    Cohabit    OthSing
  1        0        1          0          0          0
  0        1        0          1          0          0
  0        1        0          0          0          1
  1        0        0          0          1          0
  0        1        1          0          0          0
  1        0        0          0          1          0

基本上,如果您有1个女性,则男性为0,反之亦然。婚姻状况也是如此,如果男性/女性已婚,那么它就不可能单身或同居。我想使用包含4个酒吧(婚姻状况)的Plotly绘制条形图,我不在乎它是男性还是女性,我只想知道有多少人是单身,已婚,同居或其他类型单人。我的问题是每个栏都来自另一列,我还找不到这样做的方法。下面的输出示例:

“所需的输出”

有人可以给我一个想法吗?

I have a dataset like this:

Female    Male    Single    Married    Cohabit    OthSing
  1        0        1          0          0          0
  0        1        0          1          0          0
  0        1        0          0          0          1
  1        0        0          0          1          0
  0        1        1          0          0          0
  1        0        0          0          1          0

Where, basically, if you have 1 for Female, it's gonna be 0 for Male and vice versa. The same happens with the marital status, if the male/female is married, then it can't be single or cohabiting. I want to plot a bar chart using plotly that contains 4 bars (the marital status), and I don't care about if it's male or female, I just want to know how many people are single, married, cohabiting, or another kind of single. My problem is that each bar comes from a different column and I couldn't find a way to do that yet. Output example below:

desired output

Could someone give me an idea if it's possible to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2025-02-20 15:59:45

也许对于将来需要的人来说,我设法通过创建一个我需要的列索引,另一列的索引来绘制它,每列的计数,然后绘制列表,例如:

index_mar_status = df_data.columns[2:6].values

count_mar_status = [df_data[mar_status].value_counts()[1] for mar_status in index_mar_status]

bar = px.bar(x=index_mar_status,
             y=count_mar_status,
             color=count_mar_status,
             color_continuous_scale=px.colors.sequential.Pinkyl,
             title='Count of Marital Status')

bar.update_layout(yaxis_title='Number of People',
                  coloraxis_showscale=False)
bar.show()

Maybe for someone that needs that in the future, I managed to plot it by creating a list with the indexes of the columns I needed and another one with the counts for each column and then plotted the lists, like that:

index_mar_status = df_data.columns[2:6].values

count_mar_status = [df_data[mar_status].value_counts()[1] for mar_status in index_mar_status]

bar = px.bar(x=index_mar_status,
             y=count_mar_status,
             color=count_mar_status,
             color_continuous_scale=px.colors.sequential.Pinkyl,
             title='Count of Marital Status')

bar.update_layout(yaxis_title='Number of People',
                  coloraxis_showscale=False)
bar.show()
遗心遗梦遗幸福 2025-02-20 15:59:45

尝试此尝试,使用熊猫图:

ax = df.T[2:].sum(axis=1).plot.bar(rot=0)
ax.set_ylim(0,2.5)
ax.grid(axis='y')
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

输出:

”在此处输入图像说明”

Try this, using pandas plot:

ax = df.T[2:].sum(axis=1).plot.bar(rot=0)
ax.set_ylim(0,2.5)
ax.grid(axis='y')
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)

Output:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文