3

I am trying to create a figure with 6 sub-plots in python but I am having a problem. Here is a simplified version of my code:

import matplotlib.pyplot as plt
import numpy

g_width = 200
g_height = 200
data = numpy.zeros(g_width*g_height).reshape(g_height,g_width)

ax1 = plt.subplot(231)
im1 = ax1.imshow(data)
ax2 = plt.subplot(232)
im2 = ax2.imshow(data)
ax3 = plt.subplot(233)
im3 = ax3.imshow(data)
ax0 = plt.subplot(234)
im0 = ax0.imshow(data)
ax4 = plt.subplot(235)
im4 = ax4.imshow(data)
ax5 = plt.subplot(236)
ax5.plot([1,2], [1,2])
plt.show()

The above figure has 5 "imshow-based" sub-plots and one simple-data-based sub-plot. Can someone explain to me why the box of the last sub-plot does not have the same size with the other sub-plots? If I replace the last sub-plot with an "imshow-based" sub-plot the problem disappears. Why is this happening? How can I fix it?

2
  • Do you mean that the actual plotting area is smaller? This might be due to the y-axis values. In that case, you can use subplots_adjust Commented Sep 18, 2013 at 14:24
  • Does this answer: stackoverflow.com/questions/5083763/… help you? Commented Sep 18, 2013 at 14:34

1 Answer 1

2

The aspect ratio is set to "equal" for the 5imshow()calls (check by callingax1.get_aspect()) while forax5it is set toautowhich gives you the non-square shape you observe. I'm guessingimshow()` defaults to equal while plot does not.

To fix this set all the axis aspect ratios manually e.g when creating the plot ax5 = plt.subplot(236, aspect="equal")

On a side node if your creating many axis like this you may find this useful:

fig, ax = plt.subplots(ncols=3, nrows=2, subplot_kw={'aspect':'equal'})

Then ax is a tuple (in this case ax = ((ax1, ax2, ax3), (ax4, ax5, ax6))) so to plot in the i, j plot just call

ax[i,j].plot(..)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.