为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕尼黑8549860 2021-06-08 16:41:44
昨天,我将 ggplot 安装到了我的 anaconda 环境中。当我尝试使用在安装 ggplot 之前工作的 matplotlib 图时,出现以下错误。我也收到来自其他内联 jupyter 实验室代码的错误。任何帮助将不胜感激。我是可视化数据的新手。如果我应该使用另一个绘图模块,请告诉我。plt.rcParams['figure.dpi'] = 200plt.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 ax1ax2.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 clippedfig.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'
查看完整描述

2 回答

?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

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


查看完整回答
反对 回复 2021-06-16
?
白猪掌柜的

TA贡献1893条经验 获得超10个赞

对于签名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), 

           bbox_transform=ax1.transAxes, prop={'size':5})


查看完整回答
反对 回复 2021-06-16
  • 2 回答
  • 0 关注
  • 206 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号