【发布时间】:2018-09-12 19:15:44
【问题描述】:
昨天,我将 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'
【问题讨论】:
-
在安装 ggplot 期间,您可能更改了 matplotlib 版本。似乎 ggplot 与许多其他库有很大的冲突。例如,我完全无法使用最新的 matplotlib 2.2.3 运行它。
-
ggplot 的开发似乎在 2016 年就停止了。可能有一个替代方案,plotnine,但我还没有测试过。
-
感谢您的评论。我确实将我的 matplotlib 升级到了最新版本,所以现在我的情节可以工作了!我会看看plotnine。 ggplot 的部分吸引力在于希望我可以与使用 R 的同事合作。
标签: python matplotlib anaconda python-ggplot jupyter-lab