0

I've been wondering this forever: How can I set graphs in Matplotlib to share axes when I use the plt.subplot(221) (for example) command? Rather than saying:

fig, axes = plt.suplots(2,2,sharex=True, sharey=True)

I would like to say:

plt.subplot(221, sharex=True, sharey=True)

So that I don't have to deal with this annoying array that plt.subplots returns, but obviously this doesn't work.

1 Answer 1

1

You can use tuple unpacking to avoid creating the axes array:

fig, ((ax1, ax2), (ax3, ax4)) = plt.suplots(2, 2, sharex=True, sharey=True)

Afterwards you can plot as usual.

e.g. ax2.plot(...) will then plot to the top right subplot.

Sign up to request clarification or add additional context in comments.

1 Comment

I am not sure, I don't know a way. However in my opinion the subplots command results in much cleaner code. If you have so many subplots that the tuple unpacking becomes unreadable, I find axes[2,2].plot(...) nicer than plt.subplot(221, sharex=True, sharey=True)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.