【问题标题】:Merging subplots error - 'AxesSubplot' object has no attribute 'get_gridspec'合并子图错误 - 'AxesSubplot' 对象没有属性 'get_gridspec'
【发布时间】:2019-01-15 10:27:14
【问题描述】:

使用 python 2.7x,无法升级,因为我在托管 Windows 系统上。我正在尝试按照此示例合并子图,但返回以下错误:

'AxesSubplot' 对象没有属性 'get_gridspec'

我查看了此处的其他示例,但找不到任何适用于我正在使用的构建(Anaconda 2.7,Spyder IDE)的示例来自:

https://matplotlib.org/tutorials/intermediate/gridspec.html

fig9 = plt.figure(constrained_layout=False)
gs1 = fig9.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.48,
wspace=0.05)
f9_ax1 = fig9.add_subplot(gs1[:-1, :])
f9_ax2 = fig9.add_subplot(gs1[-1, :-1])
f9_ax3 = fig9.add_subplot(gs1[-1, -1])
gs2 = fig9.add_gridspec(nrows=3, ncols=3, left=0.55, right=0.98,
hspace=0.05)
f9_ax4 = fig9.add_subplot(gs2[:, :-1])
f9_ax5 = fig9.add_subplot(gs2[:-1, -1])
f9_ax6 = fig9.add_subplot(gs2[-1, -1]) 

【问题讨论】:

  • 我已将 pythonpython-3.x 标记为这个问题,DavidG 的回答似乎是所有用户都应该了解的问题

标签: python python-3.x python-2.7 matplotlib plot


【解决方案1】:

documentationfigure.add_gridspec是在matplotlib 3及以上版本中添加的。

对于 Python 3.5 及更高版本,您只需将 matplotlib 更新到最新版本,该示例即可运行。但是,matplotlib 3 不适用于 Python 2.7 或 Python 3.4 及以下版本。

因此,您可以使用 old documentation 中描述的旧方法

import matplotlib.gridspec as gridspec

gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.05, right=0.48, wspace=0.05)
ax1 = plt.subplot(gs1[:-1, :])
ax2 = plt.subplot(gs1[-1, :-1])
ax3 = plt.subplot(gs1[-1, -1])

【讨论】:

  • 哈哈,我只是在编辑模式给你的答案添加import matplotlib.gridspec as gridspec
  • @Bazingaa 谢谢。我总是忘记如何导入 gridspec,所以我想这是一个重要的部分!
  • 确实如此。来自我的 +1!
  • 所以这带来了一些新的东西 - __init__() 得到了一个意想不到的关键字参数 'constrained_layout' 我现在正在看看其他一些答案!
  • @8556732 我无法重现该错误。你能提供给你错误的代码吗?
猜你喜欢
  • 2019-04-22
  • 1970-01-01
  • 2017-02-13
  • 2016-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
相关资源
最近更新 更多