【问题标题】:Plot multiple columns values against one column针对一列绘制多列值
【发布时间】:2020-04-23 09:28:07
【问题描述】:

现在我想将这个数据框绘制在一个图中,如下所示。 EYE_WIDTH 变量与 LANE 和 SLOT_ID 的组合结合使用。如果有任何其他方式可视化这一点,欢迎提出建议。我试图通过添加绘图命令进行绘图,但所有 slot_Ids 相互重叠,因此信息模糊

print(df_new[['LANE','SLOTID','EYE_WIDTH']].groupby(['LANE','SLOTID']).mean().unstack())


          EYE_WIDTH                                                              \
  SLOTID   N0CP0.CP N0CP1.CP N0CP2.CP   N1CP1.CP   N2CP0.CP   N2CP1.CP N2CP2.CP   
  LANE                                                                            
   0       59.710345     50.7     58.5  60.320000  60.908824  57.385714     66.3   
   1       61.996552     58.5     62.4  63.180000  59.417647  61.285714     66.3   
   2       60.113793     66.3     58.5  60.450000  61.367647  59.614286     58.5   
   3       63.610345     54.6     62.4  59.800000  58.614706  59.057143     66.3   
   4       62.131034     54.6     62.4  62.010000  61.482353  61.285714     54.6   
   5       59.306897     58.5     54.6  62.920000  61.482353  64.628571     74.1   
   6       61.324138     66.3     58.5  59.800000  60.679412  60.171429     50.7   
   7       57.289655     66.3     62.4  59.800000  57.238235  56.271429     58.5   
   8       61.189655     62.4     54.6  61.100000  61.826471  62.400000     70.2   
   9       62.803448     62.4     62.4  63.050000  60.105882  62.400000     58.5   
   10      62.400000     62.4     62.4  60.970000  61.023529  60.171429     66.3   
   11      62.668966     62.4     54.6  61.360000  60.908824  63.514286     58.5   
   12      61.862069     50.7     58.5  62.903226  61.367647  61.285714     66.3   
   13      60.786207     54.6     54.6  60.450000  60.450000  57.385714     58.5   
   14      59.979310     54.6     58.5  62.270000  61.482353  60.728571     62.4   
   15      59.172414     50.7     58.5  57.850000  58.155882  60.171429     66.3   

                                                                    \
  SLOTID N3CP1.CP   N3CP2.CP   N4CP0.CP   N4CP1.CP   N4CP2.CP   N5CP1.CP   
  LANE                                                                     
   0          60.0  65.742857  60.419048  59.313913  60.026087  60.105882   
   1          65.7  64.628571  60.041129  60.466957  60.252174  63.937226   
   2          61.9  60.728571  59.410000  59.407759  60.139130  63.737956   
   3          60.3  61.842857  57.720000  59.692562  60.026087  60.407299   
   4          63.9  65.742857  60.710000  60.562810  60.365217  63.345455   
   5          60.1  57.385714  60.320000  60.466116  60.139130  58.559091   
   6          58.4  59.057143  58.375200  61.046281  60.817391  56.638636   
   7          59.6  59.057143  58.240000  58.016529  58.952174  59.740909   
   8          62.5  61.842857  61.262500  62.206612  59.178261  62.488636   
   9          61.1  59.614286  61.717500  60.060000  58.895652  59.800000   
   10         61.6  58.500000  58.792500  60.450000  57.482609  59.740909   
   11         60.9  60.171429  61.295000  59.215000  60.365217  63.404545   
   12         63.1  65.742857  63.050000  62.595000  60.421739  61.868182   
   13         59.8  64.071429  60.677500  59.507500  59.800000  60.568182   
   14         62.7  61.285714  58.662500  55.867500  57.086957  60.184091   
   15         56.9  62.957143  55.631405  58.110000  57.369565  58.381818   


    SLOTID   N5CP2.CP   N6CP0.CP   N6CP1.CP   N6CP2.CP   N7CP1.CP   N7CP2.CP  
    LANE                                                                      
    0       60.515000  60.547500  59.157831  58.444286  58.663636  58.755738  
    1       58.175000  60.547500  61.836145  59.001429  64.390909  63.161719  
    2       63.765000  59.637500  58.687952  57.274286  62.858824  60.328125  
    3       61.880000  61.620000  60.708434  59.447143  62.345455  59.444531  
    4       62.725000  59.020000  59.641463  59.280000  62.509091  60.987402  
    5       59.020000  60.417500  61.037349  60.672857  59.372727  59.229268  
    6       60.905000  57.790909  60.802410  60.282857  59.481818  58.627869  
    7       59.215000  60.216000  58.922892  60.282857  61.328873  59.746721  
    8       62.272131  60.060000  59.110843  60.338571  61.500000  61.089344  
    9       60.905000  62.176230  57.936145  56.215714  59.863636  59.522951  
    10      61.295000  61.425000  60.097590  55.212857  58.090909  58.755738  
    11      60.418033  59.897500  57.976829  58.722857  59.536364  59.267213  
    12      62.270000  57.942857  62.306024  58.270588  64.336364  61.440984  
    13      61.880000  61.295000  59.063855  60.863636  58.854545  61.760656  
    14      63.310000  58.467500  58.687952  58.155882  62.972727  60.386066  
    15      58.890000  58.077500  59.956627  54.265714  57.081818  58.979508 

【问题讨论】:

    标签: python-3.x pandas matplotlib seaborn


    【解决方案1】:

    如果你正在做我认为你想要的,那么 y 轴上的值似乎没有意义,你只想保留值之间的整体关系?

    如果是这样,那么您可以对每一列进行规范化,以便将值包含在定义的间隔中(此处为 [–0.5, 0.5]),然后通过递增索引绘制每一列的偏移量。

    fig, ax = plt.subplots()
    for i,col in enumerate(df):
        #normalize the data so that all the data is contained in the interval [-0.5, 0.5]
        norm = (df[col]-df[col].min())/(df[col].max()-df[col].min()) - 0.5
        ax.plot(df.index,i+norm)
    ax.set_yticks(range(df.columns.size))
    ax.set_yticklabels(df.columns)
    

    如果您想保留 y 值,那么另一种选择是创建一堆子图:

    fig, axs = plt.subplots(df.columns.size, 1, gridspec_kw=dict(hspace=0), sharex=True)
    for col,ax in zip(df,axs):
        ax.plot(df.index, df[col])
        ax.set_ylabel(col, rotation=0, ha='right', va='center')
    fig.tight_layout()
    

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-03
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多