new pythonic style for shared axes square subplots in matplotlib?
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()
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.
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.