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

Trouble with matplotlib after installing ggplot

昨天,我将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'

追溯表明缺少两个 "required" 参数,尽管根据 documentation,它们实际上是可选的。如果您在安装新模块后遇到此问题,那么您可能已将 matplotlib 降级到以前的版本,其中两个参数是必需的。如果是这种情况,您可能需要从控制台 pip install matplotlib --upgrade

matplotlib.figure.Figure.legend 的签名是 matplotlibin version 2.0.2

legend(handles, labels, *args, **kwargs)

in version 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})