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()

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.

Leave a Comment