安装ggp​​lot后matplotlib出现问题

昨天,我将 ggplot 安装到了我的 anaconda 环境中。当我尝试使用在安装 ggplot 之前工作的 matplotlib 图时,出现以下错误。我也收到来自其他内联 jupyter 实验室代码的错误。任何帮助将不胜感激。我是可视化数据的新手。如果我应该使用另一个绘图模块,请告诉我。


plt.rcParams['figure.dpi'] = 200

plt.rcParams.update({'font.size': 5})


fig, ax1 = plt.subplots()

ax1.set_xlabel('Time')


ax1.set_ylabel('price', color='k')

ax1.plot(df['price'], color='#0072b5', label = 'price')

ax1.tick_params(axis='y', labelcolor='k')

#ax1.tick_params(axis='x',  labelrotation = 90) 


ax2 = ax1.twinx()  # instantiate a second axes that shares the same x-axis#


color = 'tab:cyan'

ax2.set_ylabel('gen', color='k')  # we already handled the x-label with ax1

ax2.plot(df['gen'], color='#e2e3e2', label = 'gen')

ax2.tick_params(axis='y', labelcolor='k')


#ax1.legend(loc=2)

#ax2.legend(loc=1)

fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax1.transAxes, prop={'size':5})



fig.tight_layout()  # otherwise the right y-label is slightly clipped

fig.suptitle('%s, %s %s' % (df, month_graph, year_graph) , fontsize=8)

fig.subplots_adjust(top=0.90)

plt.savefig("%s.png" % ('genPrice'))

plt.show()


---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

<ipython-input-14-032d973b53a3> in <module>()

     19 #ax1.legend(loc=2)

     20 #ax2.legend(loc=1)

---> 21 fig.legend(loc=1, bbox_to_anchor=(1,1), bbox_transform=ax1.transAxes, prop={'size':5})

     22 

     23 


TypeError: legend() missing 2 required positional arguments: 'handles' and 'labels'


慕尼黑8549860
浏览 207回答 2
2回答

阿波罗的战车

回溯指出缺少两个“必需”参数,尽管根据文档,它们实际上是可选的。如果您在安装新模块后遇到此问题,那么您可能已将 matplotlib 降级到必须使用两个参数的先前版本。如果是这种情况,您可能需要pip install matplotlib --upgrade从控制台。

白猪掌柜的

对于签名matplotlib.figure.Figure.legend是在2.0.2版本matplotlib的legend(handles, labels, *args, **kwargs)而在 2.1.2或更高版本中它是legend(*args, **kwargs)这意味着您在安装 ggplot 期间降级了 matplotlib。如果您想继续使用这个较旧的 matplotlib 版本,您需要自己提供句柄和标签。这可能看起来像h1, l1 = ax1.get_legend_handles_labels()h2, l2 = ax2.get_legend_handles_labels()fig.legend(h1+h2, l1+l2, loc=1, bbox_to_anchor=(1,1),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;bbox_transform=ax1.transAxes, prop={'size':5})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python