如何删除Dataframe.plot中的特定图例标签?

时间:2018-04-08 03:34:13

标签: pandas dataframe matplotlib plot legend

我试图通过' bar'一起绘制两个DataFrame。风格和'线'样式分别,但仅在条形图中显示图例时出现问题,不包括直线。

以下是我的代码:

import numpy as np
import pandas as pd

np.random.seed(5)
df = pd.DataFrame({'2012':np.random.random_sample((4,)),'2014':np.random.random_sample((4,))})
df.index = ['A','B','C','D']
sumdf = df.T.apply(np.sum,axis=1)
ax = df.T.plot.bar(stacked=True)
sumdf.plot(ax=ax)
ax.set_xlim([-0.5,1.5])
ax.set_ylim([0,3])
ax.legend(loc='upper center',ncol=3,framealpha=0,labelspacing=0,handlelength=4,borderaxespad=0)

恼人地得到了这个:Figure,其中线条图例也显示在图例框中。我想删除它而不是让它不可见。

但我找不到路。

谢谢!

1 个答案:

答案 0 :(得分:5)

如果matplotlib.legend's标签以下划线开头,默认情况下它不会显示在图例中。

您只需更改

即可
sumdf.plot(ax=ax)

sumdf.plot(ax=ax, label='_')

enter image description here