Python 从数据框Groupby创建等高线图

Python 从数据框Groupby创建等高线图,python,matplotlib,pandas,group-by,contour,Python,Matplotlib,Pandas,Group By,Contour,我有以下数据帧: In [66]: hdf.size() Out[66]: a b 0 0.0 21004 0.1 119903 0.2 186579 0.3 417349 0.4 202723 0.5 100906

我有以下数据帧:

In [66]: hdf.size()
Out[66]:
a           b
0           0.0          21004
            0.1         119903
            0.2         186579
            0.3         417349
            0.4         202723
            0.5         100906
            0.6          56386
            0.7           6080
            0.8           3596
            0.9           2391
            1.0           1963
            1.1           1730
            1.2           1663
            1.3           1614
            1.4           1309
...
186         0.2         15
            0.3          9
            0.4         21
            0.5          4
187         0.2          3
            0.3         10
            0.4         22
            0.5         10
188         0.0         11
            0.1         19
            0.2         20
            0.3         13
            0.4          7
            0.5          5
            0.6          1
Length: 4572, dtype: int64
你看,a从0…188,b在每组中从某个值到某个值。作为指定的Z值,a/b对出现的计数

如何从分组的数据框中获取计数图或热图图

我有这样一个问题(询问答案?):

如何从Groupby对象中获取x和y值并绘制轮廓?

欢迎使用SO

很明显,对于您的每个“a”级别,“b”级别的数量并不相同,因此我将建议以下解决方案:

In [44]:

print df #an example, you can get your dataframe in to this by rest_index()
    a  b     value
0   0  1  0.336885
1   0  2  0.276750
2   0  3  0.796488
3   1  1  0.156050
4   1  2  0.401942
5   1  3  0.252651
6   2  1  0.861911
7   2  2  0.914803
8   2  3  0.869331
9   3  1  0.284757
10  3  2  0.488330

[11 rows x 3 columns]
In [45]:
#notice that you will have some 'NAN' values
df=df.pivot('a', 'b', 'value')
In [46]:

X=df.columns.values
Y=df.index.values
Z=df.values
x,y=np.meshgrid(X, Y)
plt.contourf(x, y, Z) #the NAN will be plotted as white spaces
Out[46]:
<matplotlib.contour.QuadContourSet instance at 0x1081385a8>
[44]中的

print df#例如,您可以通过rest_index()将数据帧放入其中
a b值
0   0  1  0.336885
1   0  2  0.276750
2   0  3  0.796488
3   1  1  0.156050
4   1  2  0.401942
5   1  3  0.252651
6   2  1  0.861911
7   2  2  0.914803
8   2  3  0.869331
9   3  1  0.284757
10  3  2  0.488330
[11行x 3列]
在[45]中:
#请注意,您将有一些“NAN”值
df=df.pivot('a','b','value')
在[46]中:
X=df.columns.values
Y=df.index.values
Z=df.0值
x、 y=np.meshgrid(x,y)
plt.contourf(x,y,Z)#NAN将绘制为空白
出[46]:

非常感谢!我的错误是,我没有意识到,我必须对groupby数据帧应用一些函数,比如
.size()
,才能使用它

hdf = aggdf.groupby(['a','b']).size()
hdf
给我

a           b
1           -2.0          1
            -1.9          1
            -1.8          1
            -1.7          2
            -1.6          5
            -1.5         10
            -1.4          9
            -1.3         21
            -1.2         34
            -1.1         67
            -1.0         65
            -0.9         94
            -0.8        180
            -0.7        242
            -0.6        239
...
187          0.4        22
             0.5        10
188         -0.6         2
            -0.5         2
            -0.4         1
            -0.3         2
            -0.2         5
            -0.1        10
            -0.0        18
             0.1        19
             0.2        20
             0.3        13
             0.4         7
             0.5         5
             0.6         1
Length: 8844, dtype: int64
有了这些,再加上你的帮助,我就可以做到了

hdfreset = hdf.reset_index()
hdfreset.columns = ['a', 'b', 'occurrence']
hdfpivot=hdfreset.pivot('a', 'b')
这最终给了我正确的值

X=hdfpivot.columns.levels[1].values
Y=hdfpivot.index.values
Z=hdfpivot.values
Xi,Yi = np.meshgrid(X, Y)
plt.contourf(Yi, Xi, Z, alpha=0.7, cmap=plt.cm.jet);
这导致了这一美丽的轮廓:


非常感谢!可以用零(Z)填充b的不可用值。我忘了这一点,但我认为这会使它更容易,不是吗?当然你可以使用
pandas
中的
fillna()
来做这件事。但我认为这取决于0是否是数据的一个可能值(实际上意味着什么),我没有让Groupby对象为我工作
无法访问“DataFrameGroupBy”对象的可调用属性“reset_index”,请尝试使用“apply”方法
伟大的解决方案,您可以接受自己的答案,仅供参考。如何将此示例扩展到meshgrid>2维?
X=hdfpivot.columns.levels[1].values
Y=hdfpivot.index.values
Z=hdfpivot.values
Xi,Yi = np.meshgrid(X, Y)
plt.contourf(Yi, Xi, Z, alpha=0.7, cmap=plt.cm.jet);