2

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?

FluidFox
  • 107
  • 10

1 Answers1

2

You are specifically asking matplotlib not to keep the axes the same size by calling plt.tight_layout().

Remove plt.tight_layout() and your axes should keep the same size, independent on whether there are labels or not.

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • That has improved things, thank you. However, now my titles and axis labels are overlapping and adding plt.subplots_adjust(wspace=10) doesn't seem to help. I also get some left right movement when I add the colorbar. – FluidFox Oct 16 '17 at 14:15
  • Those are common problems to which there are also common solutions. The above answers the question though. If you want to ask something else, ask that something else - if the solutions found on SO do not help, make clear in how far they aren't. – ImportanceOfBeingErnest Oct 16 '17 at 14:20
  • The solution I found on SO for overlapping titles and axis labels was to use, plt.tight_layout(), but that is what I have now removed. An alternative answer suggested plt.subplots_adjust(wspace=10), but that does not have an effect for me. I will look in to the colorbar problem. – FluidFox Oct 16 '17 at 14:23
  • Concerning the colorbar, my favourite solution would be the one named "using subplots" in [this answer](https://stackoverflow.com/a/43425119/4124317). For the subplots_adjust, why not print the parameters obtained when using `tight_layout` in the case with all elements in the plot and reuse them also for the case when there are not all elements in. – ImportanceOfBeingErnest Oct 16 '17 at 14:31
  • Having looked at the answer linked above I eventually recast my entire plot using the plot.subplots as described, removing the need for the tight layout and problems that arose from it. As your answer solved my initial problem and led (slightly indirectly) to a better overall fix I'll mark it as accepted. Thanks. – FluidFox Oct 19 '17 at 13:55