Python Pandas MultiIndex Plot的图例颜色不正确

时间:2016-03-16 18:31:07

标签: python pandas matplotlib legend multi-index

当尝试从Python Pandas MultiIndex中绘制信息时,这似乎是一个真实且重复(并且未解决)的问题。

绘图代码:

df.unstack(0).plot(x='SHIFT', y='INFORMATION_SURPLUS_PCT')

将呈现以下图表:

Pandas MultiIndex graph

不幸的是,图例线条的颜色与图表上的线条不匹配,我还尝试使用以下代码来提供图例,但这仍然不匹配:

df.unstack(0).plot(x='SHIFT', y='INFORMATION_SURPLUS_PCT', legend=df.index.levels)

示例df:

       INFORMATION_SURPLUS_DIFF  INFORMATION_SURPLUS_PCT  \
   SYMBOL                                                         
   LBTYB  0                   0.000000                 0.000000   
          1                   0.015046                11.206891   
          2                   0.042249                31.468118   
          3                   0.005421                 4.037495   
          4                  -0.005569                 0.000000   
          5                  -0.069742                 0.000000   
          6                   0.036281                27.022972   
          7                   0.072626                54.093432   
          8                  -0.026966                 0.000000   
          9                  -0.048241                 0.000000   
          10                  0.145121               108.088780   
   MNST   0                   0.000000                 0.000000   
          1                   0.062962                 0.000000   
          2                   0.029280                 0.000000   
          3                   0.056574                63.569509   
          4                   0.088341                99.265331   
          5                   0.052083                 0.000000   
          6                   0.041253                46.353665   
          7                   0.058658                65.911282   
          8                   0.080748                90.732985   
          9                   0.031419                 0.000000   
          10                  0.055326                62.167628   

              MUTUAL_INFORMATION  SHIFT  
   SYMBOL                                
   LBTYB  0             0.134261      0  
          1             0.149307     -1  
          2             0.176510     -2  
          3             0.139681     -3  
          4             0.128692     -4  
          5             0.064518     -5  
          6             0.170542     -6  
          7             0.206887     -7  
          8             0.107294     -8  
          9             0.086020     -9  
          10            0.279381    -10  
   MNST   0             0.088995      0  
          1             0.151957     -1  
          2             0.118276     -2  
          3             0.145569     -3  
          4             0.177337     -4  
          5             0.141078     -5  
          6             0.130248     -6  
          7             0.147653     -7  
          8             0.169743     -8  
          9             0.120414     -9  
          10            0.144322    -10  

2 个答案:

答案 0 :(得分:1)

我想你可以试试:

fig, ax = plt.subplots(figsize=(8,6))
for i, grp in df.groupby(level=0):
    grp.plot(x='SHIFT', y='INFORMATION_SURPLUS_PCT', label=str(i), ax=ax)

graph

答案 1 :(得分:0)

此代码片段将起作用,它涉及对MultiIndex进行分组并在循环中绘制相同的图形 - 打开以进行改进:

grouped = df.groupby(level=0)
ax = plt.figure()
first = True
for i, group in grouped:
    print i
    if first:
        ax = group.plot(x='SHIFT', y='INFORMATION_SURPLUS_PCT', label=str(i))
        first = False
    else:
        group.plot(ax=ax, x='SHIFT', y='INFORMATION_SURPLUS_PCT', label=str(i))

新情节图片:

enter image description here