L. *_* Li 3 plot matplotlib legend dataframe pandas
我试图分别通过'bar'样式和'line'样式将两个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)
Run Code Online (Sandbox Code Playgroud)
令人讨厌的是:Figure,图例框中还显示了行图例。我要删除它,而不是使其不可见。
但是我找不到路。
谢谢!
如果matplotlib.legend的标签以下划线开头,则默认情况下,该标签不会显示在图例中。
你可以简单地改变
sumdf.plot(ax=ax)
Run Code Online (Sandbox Code Playgroud)
至
sumdf.plot(ax=ax, label='_')
Run Code Online (Sandbox Code Playgroud)