Dataframe创建等高线图

Dataframe创建等高线图

本文介绍了从Pandas Groupby Dataframe创建等高线图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下熊猫数据框:

  p> 

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

给我

  ab 
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
长度:8844,dtype:int64

,和你的帮助CT朱,然后我可以做

  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);

这导致了这个美丽的轮廓:




I have following Pandas Dataframe:

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

You see, a from 0...188 and b in every group from some value to some value. And as the designated Z-value, the count of the occurence of the pair a/b.

How to get a countour or heatmap plot out of the grouped dataframe?

I have this (asking for the ?):

numcols, numrows = 30, 30
xi = np.linspace(0, 200, numcols)
yi = np.linspace(0, 6, numrows)
xi, yi = np.meshgrid(xi, yi)
zi = griddata(?, ?, hdf.size().values, xi, yi)

How to get the x and y values out of the Groupby object and plot a contour?

解决方案

Thanks a lot! My fault was, that I did not realize, that I have to apply some function to the groupby dataframe, like .size(), to work with it...

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

gives me

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

With that, and your help CT Zhu, I could then do

hdfreset = hdf.reset_index()
hdfreset.columns = ['a', 'b', 'occurrence']
hdfpivot=hdfreset.pivot('a', 'b')

and this finally gave me the correct values to

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);

which leads to this beautiful contourf:

这篇关于从Pandas Groupby Dataframe创建等高线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:56