15

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.

Community
  • 1
  • 1

2 Answers2

20

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.

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
2

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.

cronos
  • 2,268
  • 16
  • 17
  • But I want the axes to be shared *and* the plots to be square. How can that be achieved? As you note, `datalim` and `equal` aspect do not solve the problem. This must have a very simple solution? –  Nov 29 '12 at 18:26
  • I am not sure, but I am afraid it is not possible since one gets `ValueError: adjustable must be "datalim" for shared axes` if one tries to use "box" and "equal". – cronos Nov 30 '12 at 09:42
  • so what is a way to make a square subplot in matplotlib with shared axes? It's such a trivial plot and matplotlib so powerful, there must be a way to do it? –  Dec 01 '12 at 02:22
  • 1
    Got mislead by the error message ("must be"). @JoeKington got it right, `adjustabe='box-forced'`, does the job. Works also fine with the `subplot_kw` dictionary. – cronos Dec 02 '12 at 16:58