【问题标题】:Trouble with matplotlib after installing ggplot安装 ggplot 后 matplotlib 出现问题
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 感谢您的评论。我很欣赏代码的链接。你怎么知道在哪里可以找到它?代码中列出的 cmets 是典型的 python 模块吗?你的回答很有见地。我以前从未见过这样的事情。谢谢。
  • @getaglow 有一种方法可以让您访问任何函数 function_name 的文档字符串,如果存在文档字符串,则从 Python:调用 help(function_name) 或者,如果 funcion_name 是上课ClassName,打电话给help(ClassName.function_name)。在这种情况下,您将在 import matplotlib 之后调用 help(matplotlib.figure.Figure)(不是 matplotlib.pyplot)。如果你喜欢这个答案,你可以选择它:)
  • 谢谢。您的回答帮助我更好地理解了我在编码时所做的事情,这将使我变得更好。
【解决方案2】:

matplotlib.figure.Figure.legend 的签名是 matplotlib 的 in 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})

【讨论】:

  • 谢谢。我升级了我的 matplotlib,它似乎已经解决了这个问题。我还没有尝试过使用 ggplot。当我今天晚些时候有时间时,我可能会尝试使用您的建议。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-07-23
  • 2012-11-29
  • 2012-08-16
  • 1970-01-01
  • 2022-08-19
  • 2011-01-28
  • 2016-12-05
  • 1970-01-01
相关资源
最近更新 更多