I have a matplotlib figure with two subfigures, one above the other. I am producing a set of similar figures, but only want the axes, labels, titles etc. on the first of the set. I want to then insert these in to a document next to each other so that the subplot images all line up and are the same size.
The problem I have is that when I switch the grid, axes, colorbar etc. off, the images in the subplots expand to fill the space that was previously occupied by the text/ticks.
Is there a way to remove my axis labels but keep the images the same size (e.g. perhaps so that matplotlib 'thinks' there is something there but but display it)? I have tried inserting empty titles using
plt.xlabel('')
but that doesn't work, and there is no similar method for replacing axes. Alternatively can I specify the size and location of the subfigures so their images are fixed, regardless of whether they have axes or not.
I want the images to remain the same when the variables 'colorbar' and 'grid' are set to 'off', instead of becoming larger to fill the white space.
Sample Code:
import numpy as np
import matplotlib.pyplot as plt
filename = 'outputimage.png'
grid = 'on'
colorbar = 'on'
r = np.linspace(0,1.0,11)
z = np.linspace(0,1.0,11)
data1 = np.zeros([10,10])
data2 = np.zeros([10,10])
for i in range(10):
for j in range(10):
data1[i,j] = r[i]*z[j]
data2[i,j] = -r[i]*z[j]
R, Z = np.meshgrid(r, z)
fig = plt.figure(1,facecolor="white",figsize=(3.5,10),dpi=200)
plt.subplot(211)
ax = plt.gca()
im11 = ax.pcolormesh(R, Z, data1)
ax.set_aspect('equal',adjustable='box')
if grid == 'on':
plt.axis('on')
plt.xlabel(r'r')
plt.ylabel(r'z')
plt.title('data1')
else:
plt.axis('off')
plt.xlabel(' ')
plt.ylabel(' ')
plt.title(' ')
if colorbar == 'on':
CBI = plt.colorbar(im11, orientation='vertical')
plt.subplot(212)
ax = plt.gca()
im21 = ax.pcolormesh(R, Z, data2)
ax.set_aspect('equal',adjustable='box')
plt.xlabel(r'r')
plt.ylabel(r'z')
plt.axis('off')
if colorbar == 'on':
CB = plt.colorbar(im21, orientation='vertical')
if grid == 'on':
plt.title('data2')
else:
plt.title(' ')
plt.tight_layout(h_pad=2.0)
plt.savefig(filename)
Which produces: when colorbar and grid set 'on' when colorbar and grid set 'off' Wheras I want the second image to be identical to the first, just without the colorbar and text.
NB - I am using matplotlib2tikz if there is a way to do it using this?