15
votes

Related to: plotting autoscaled subplots with fixed limits in matplotlib

I would like to make a set of subplots that are all on the same scale, using the subplots new compact style, as in http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplots and have them be square.

I tried:

fig, axes = subplots(numplots, 1, sharex=True, sharey=True, adjustable='box', aspect='equal')

But I found that these keyword arguments are not implemented in the subplots wrapper. What's the way to do it?

To reiterate, the goal is simply to have shared axes, so that all the data are on the same scale, and have the plots be square.

2

2 Answers

19
votes

Just use adjustable='box-forced' instead of adjustable='box'.

As @cronos mentions, you can pass it in using the subplot_kw kwarg (additional keyword arguments to subplots are passed on to the Figure not the Axes, thus the need for subplot_kw).

Instead, I'm going to use setp, which basically just does for item in sequence: item.set(**kwargs). (All matplotlib artists have a set method that can be used similar to matlab's set.)

Which one is the "better" approach will depend on what you're doing. A lot of people would argue that setp is very "unpythonic", but I don't see the problem with it.

As a quick example:

import matplotlib.pyplot as plt

fig, axes = plt.subplots(ncols=3, sharex=True, sharey=True)
plt.setp(axes.flat, aspect=1.0, adjustable='box-forced')

axes[0].plot(range(50))

plt.show()

enter image description here

I forget the reason for the two different adjustable box styles, at the moment. I remember that I found it really confusing the first time I came across it, and I dug through the code and there was some obvious reason for it... I can't remember what that reason was at the moment, though.

2
votes

The documentation you refer to suggests a subplot_kw

fig, axes = subplots(numplots, 1, sharex=True, sharey=True, subplot_kw=dict(adjustable='datalim', aspect='equal'))

However the shared axes seem to require datalim as adjustable, the plots are scaled correctly but not square. If you leave out the shared axes, then "box" works. Your call.