2

Per the (many) answers to this question, I should try to use plt.subplots() to create figure axes when I'm not interested in an audience of MATLAB users. One thing that isn't clear to me (and is not shown in the documentation ) is how to create subplots that span multiple axes using this invocation. How to do it other ways, e.g. by repeatedly calling add_subplot, has been discussed elsewhere.

So, how do I use plt.subplots() to instantiate a figure with subplots of different sizes?

The folowing code (source) gives the behavior I'm talking about, but with different invocations.

fig = plt.figure()

ax1 = plt.subplot(221)
ax2 = plt.subplot(223)
ax3 = plt.subplot(122)

example_plot(ax1)
example_plot(ax2)
example_plot(ax3)

plt.tight_layout()
Community
  • 1
  • 1
Mark Kelly
  • 31
  • 1
  • 5
  • 1
    What do you mean "span multiple axes"? As in multiple `Axes` objects? As in [some subplots are larger than others](http://matplotlib.org/1.4.0/users/gridspec.html#gridspec-and-subplotspec)? – wflynny May 18 '15 at 20:01
  • 1
    Could you rephrase your question as a question? It's a bit difficult to tell precisely what you are asking. – Eric Hauenstein May 18 '15 at 20:05
  • 1
    Something like the second-to-last example [here](http://matplotlib.org/examples/pylab_examples/demo_tight_layout.html) (scroll down!)? – cphlewis May 18 '15 at 20:17
  • It's like that, cphlewis, but using a different invocation than I'm asking about. I've edited the question to clarify for both you and Eric Hauenstein – Mark Kelly May 18 '15 at 21:05
  • @user35050 Is there any reason you need to use `plt.subplots()`? Are you just trying to write fewer lines? If the `Axes` objects are different sizes, you are much better off using [`gridspec`](http://matplotlib.org/1.4.0/users/gridspec.html#gridspec-and-subplotspec)/[`gridspec` with variable cell sizes](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot2grid) or [`subplot2grid`](http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.subplot2grid) – wflynny May 18 '15 at 21:43
  • 1
    The reason that I want to use `plt.subplots()` is because that seems to be the most straightforward way to use them within the object-oriented interface, and I would like to have some consistency in the objects and functions I use to make these plots. If there is no such method using `plt.subplots()`, then I'll accept that as an answer, face the music, and learn the `gridspec` interface instead. – Mark Kelly May 18 '15 at 21:51
  • 1
    At the end of the day, they're all just different ways to get a collection of `Axes` objects. **What is your actual use, i.e. how many subplots in a figure and in what arrangement?** That will determine which approach is best for you. But to my knowledge, you cannot specify anything akin to `colspan`/`rowspan` using `plt.subplots`. The best you can probably do is specify `gridspec_kw` with different height/width ratios. – wflynny May 18 '15 at 22:00

0 Answers0