问题描述
我正在使用如下数据集:
I am working with a dataset that looks as follows:
Gender, Height, WidthMale, 23.4, 4.4Female, 45.4, 4.5
Gender, Height, WidthMale, 23.4, 4.4Female, 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.
希望您能理解我的意思,这已经让我好几个小时了.
I hope you understand what I mean, I've been slamming my head at this for hours.
推荐答案
您从pandas文档中获得的示例在数据帧中具有三个独立的列,而df.hist()
为这三个列生成三个不同的直方图.您的数据结构有些不同.如果您想直接使用matplotlib,可以尝试以下方法:
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:
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:
这篇关于用Group By绘制 pandas 的堆积直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!