我有一个看起来像这样的数据集:
Country m1 m2 m3
Canada 1 43 0.2
Canada 3 43 0.5
Canada 4 41 0.1
Canada 2 46 0.3
Sweden 4 46 0.4
Sweden 2 48 0.5
Sweden 3 39 0.5
France 5 43 0.1
France 2 48 0.1
France 3 49 0.9
我想制作一个直方图,将m3分为5个容器或任何合适的容器,并将这些容器也堆叠到各个国家中。
所以0-0.1箱中的堆叠条形为2/3法国和1/3加拿大(用颜色表示,然后带有图例)。
我有以下内容:
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('data.csv')
x= df['m3']
num_bins = 5
plt.hist(x, num_bins, density=1, histtype='bar', stacked=True, label=df['Country'] )
plt.show()
但是它根本没有堆叠它。认为我在这里做错了...
答案 0 :(得分:1)
您可以将crosstab
与cut
一起使用,并按DataFrame.plot.bar
进行绘制:
df = pd.crosstab(pd.cut(df['m3'], 5), df['Country'])
print (df)
Country Canada France Sweden
m3
(0.0992, 0.26] 2 2 0
(0.26, 0.42] 1 0 1
(0.42, 0.58] 1 0 2
(0.74, 0.9] 0 1 0
df.plot.bar(stacked=True)
或将DataFrame.pivot
与DataFrame.plot.hist
一起使用:
df1 = df.pivot(columns='Country', values='m3')
print (df1)
Country Canada France Sweden
0 0.2 NaN NaN
1 0.5 NaN NaN
2 0.1 NaN NaN
3 0.3 NaN NaN
4 NaN NaN 0.4
5 NaN NaN 0.5
6 NaN NaN 0.5
7 NaN 0.1 NaN
8 NaN 0.1 NaN
9 NaN 0.9 NaN
df1.plot.hist(stacked=True, bins=5)
答案 1 :(得分:0)