使用 Pandas 和 Group By 绘制堆叠直方图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36238478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:56:39  来源:igfitidea点击:

Plotting a stacked histogram with Pandas with Group By

pythonpandasmatplotlib

提问by ayhan

I am working with a dataset that looks as follows:

我正在使用如下所示的数据集:

Gender, Height, Width Male, 23.4, 4.4 Female, 45.4, 4.5

Gender, Height, Width Male, 23.4, 4.4 Female, 45.4, 4.5

I'd like to visualize the stacked histograms of height and width. I'm hoping to have two stacked histograms per plot (one for each gender).

我想可视化高度和宽度的堆叠直方图。我希望每个图有两个堆叠的直方图(每个性别一个)。

This is the stacked Histogram from the documentation. If there was three genders, this might be a good graph for width.

这是文档中的堆叠直方图。如果有三种性别,这可能是一个很好的宽度图表。

enter image description here

在此处输入图片说明

I hope you understand what I mean, I've been slamming my head at this for hours.

我希望你明白我的意思,我一直在猛烈抨击这几个小时。

回答by ayhan

Your example from pandas documentation has three seperate columns in a dataframe and df.hist()generates three different histograms for those three columns. Your data structure is a little different. If you'd like to use matplotlib directly, you can try this:

您来自 pandas 文档的示例在数据框中具有三个单独的列,并df.hist()为这三列生成三个不同的直方图。你的数据结构有点不同。如果你想直接使用 matplotlib,你可以试试这个:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
np.random.seed(10)
df = pd.DataFrame({"Gender":np.random.choice(["Female", "Male"], 1000), 
                "Height": 30+np.random.randn(1000)*5,
                "Width": 5+np.random.randn(1000)})
df.loc[df["Gender"]=="Male", "Height"] = df.loc[df["Gender"]=="Male", "Height"] + 8

plt.hist(df[df["Gender"]=="Male"]["Height"].reset_index(drop=True), alpha=0.6, label="Male")
plt.hist(df[df["Gender"]=="Female"]["Height"].reset_index(drop=True), alpha=0.6, label="Female")
plt.legend()
plt.show()

This will create a histogram like this:

这将创建一个像这样的直方图:

enter image description here

在此处输入图片说明