matplotlib不显示图例

2024-04-26 22:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数据帧,我必须将其传输到numpy数组中,因为我想在索引上绘制,这是一个日期时间,但是我得到了这个错误:

KeyError: "DatetimeIndex(['2000-01-01', '2001-01-01', '2002-01-01', '2003-01-01',\n               '2004-01-01', '2005-01-01', '2006-01-01', '2007-01-01',\n               '2008-01-01', '2009-01-01', '2010-01-01', '2011-01-01',\n               '2012-01-01', '2013-01-01', '2014-01-01', '2015-01-01',\n               '2016-01-01', '2017-01-01'],\n              dtype='datetime64[ns]', name='year', freq=None) not in index"

这是我的数据帧:

^{pr2}$

这是我将它转换为np数组的方式,以便绘制它

    year=np.array(df12.year)
un=np.array(df12.rel_uncertain,)
neg= np.array(df12.rel_negative)

然后我在策划它

plt.plot(x,neg, label = 'neg')
plt.plot(x,pos,)

除了传说,一切都很好 我尝试使用mlp的legend函数并手动创建图例 因此我修改了代码:

negative=plt.plot(x,neg,linestyle ='dashed', label='negative')
plt.plot(x,pos,)
plt.legend(handles=[negative])

这给了我以下错误:

AttributeError: 'list' object has no attribute 'get_label'

Tags: 数据posplot错误np绘制plt数组