簡體   English   中英

熊貓分組圖值

[英]pandas groupby plot values

我有一個看起來像這樣的熊貓數據框:

      **real    I     SI     weights**
        0       1     3      0.3  
        0       2     4      0.2
        0       1     3      0.5
        0       1     5      0.5

        1       2     5      0.3  
        1       2     4      0.2
        1       1     3      0.5

我需要將其除以“真實”,然后需要執行以下操作:

給定I值,請考慮SI的每個值,然后加上總重量。 最后,對於每個實現,我都應該有這樣的東西:

    real = 0:
             I = 1     SI = 3      weight = 0.8
                       SI = 5      weight = 0.5

             I = 2     SI = 4      weight = 0.2

    real = 1:  
             I = 1     SI = 3      weight = 0.5

             I = 2     SI = 5      weight = 0.3
                       SI = 4      weight = 0.2

然后的想法是,對於I和real的每個值,在x軸上繪制SI的值,並在y軸上繪制相對總重量(標准化為1)。

我試圖做的是:

       name = ['I', 'SI','weight', 'real']
       Location = 'Simulationsdata/prova.csv'
       df = pd.read_csv(Location, names = name,sep='\t',encoding='latin1') 

       results = df.groupby(['I', 'real', 'SI']).weight.sum()

當我打印結果時,我有了想要的表,但是現在我不知道如何繪制想要的圖,因為我不知道如何獲取SI值...

完成此操作后:

results = df.groupby(['real', 'I', 'SI'])['weights'].sum()

您可以使用以下方法獲取存儲在數據框中的'real''I''SI'

results.index.get_level_values(0)
Int64Index([0, 0, 0, 1, 1, 1], dtype='int64', name='real'
results.index.get_level_values(1)
Int64Index([1, 1, 2, 1, 2, 2], dtype='int64', name=' I')
results.index.get_level_values(2)
Int64Index([3, 5, 4, 3, 4, 5], dtype='int64', name=' SI')

您可以遍歷這些圖以獲得所需的圖。 例如:

import matplotlib.pyplot as plt
fig, axes = plt.subplots(2, 2)

for idx1, i in enumerate(results.index.get_level_values(0).unique()):
    for idx2, j in enumerate(results.index.get_level_values(1).unique()):
        axes[idx1, idx2].plot(results.loc[i, j], 'o')
        axes[idx1, idx2].set_xlabel('SI')
        axes[idx1, idx2].set_ylabel('weights')
        axes[idx1, idx2].set_xlim([0, 6])
        axes[idx1, idx2].set_ylim([0, 1])
        axes[idx1, idx2].set_title('real: {} I: {}'.format(i, j))

plt.tight_layout()
plt.show()

這使

我重視真實的子圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM