Create Contour Plot from Pandas Groupby Dataframe

前端 未结 2 568
悲&欢浪女
悲&欢浪女 2021-02-02 01:23

I have following Pandas Dataframe:

In [66]: hdf.size()
Out[66]:
a           b
0           0.0          21004
            0.1         119903
            0.2               


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 01:46

    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:

    enter image description here

提交回复
热议问题