昨天,我将 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贡献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})
添加回答
举报
0/150
提交
取消