Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging subplots error - 'AxesSubplot' object has no attribute 'get_gridspec'

Using python 2.7x, cannot upgrade as I'm on a managed windows system. I'm trying to follow this example to merge subplots but return the following error:

'AxesSubplot' object has no attribute 'get_gridspec'

I've taken a look at other examples on here, but cannot find any that work with the build I'm using (Anaconda 2.7, Spyder IDE) Example from:

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]) 
like image 334
8556732 Avatar asked Sep 16 '25 04:09

8556732


1 Answers

Looking at the documentation figure.add_gridspec was added in matplotlib 3 and above.

For Python 3.5 and above you can simply update matplotlib to the latest version and the example will work. However, matplotlib 3 is not available for Python 2.7 or Python 3.4 and below.

Therefore you can use the old way as described in the 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])
like image 163
DavidG Avatar answered Sep 18 '25 16:09

DavidG