7

I'm wondering if there's an equivalent function to

fig, axarr = plt.subplots(3, 2, sharex='col', sharey='row')

where only the axes array is generated for an existing figure, instead of creating a new figure object. I basically need to create a matplotlib window, populate it, and update it when a button has been pressed. I would like to use the subplots method as it allows sharing axes, but it forces the creation of a new figure object, which would open a new window.

My current code looks like this:

fig = plt.figure()

# When button is pressed
fig.clear()
for i in range(6):
    ax = fig.add_subplot(3, 2, i+1)
    ax.plot(x, y)
plt.draw()

And I'd like to do something like

fig, axarr = plt.subplots(3, 2, sharex='col', sharey='row')

# When button is pressed
fig.clear()
axarr = fig.subplots(3, 2, sharex='col', sharey='row')
for i in range(3):
    for j in range(2):
        ax = axarr[i][j]
        ax.plot(x, y)
plt.draw()

Alternatively, is there a way to directly work with the matplotlib windows? I could plot the new figure object into the existing window if that is an option as well.

I am using the PySide backend if that makes any difference. Thanks.

3
  • As far as I know, you either use fig, axarr = plt.subplots(...) or create the figure yourself and add the subplots manually. If I remember correctly, plt.subplots is actually adding each new axis for you under the hood in the pyplot source code, i.e. according to your first example. Commented Jul 6, 2016 at 19:17
  • Yes but the subplots method is able to hide certain axis labels, and also link certain subplots together when the interactive tools such as zoom are used. I'm not sure how to replicate these bevaviours. Commented Jul 6, 2016 at 19:22
  • If you want to link two subplots together you can do that with the keyword argument sharex = ax_??? or sharey when you call add_subplot. I.e. ax2 = fig.add_subplots(2, 1, 2, sharex=ax1) if ax1 is already created. Commented Jul 6, 2016 at 19:30

2 Answers 2

3

A dirty workaround:

you can use the keyword num to specify the figure handle explicitly:

import matplotlib.pyplot as plt
fig1, axes1 = plt.subplots(5, 4, gridspec_kw={'top': 0.5}, num=1)
fig2, axes2 = plt.subplots(5, 4, gridspec_kw={'bottom': 0.5}, num=1)
plt.show()

This gives:

In [2]: fig1 is fig2
Out[2]: True

In [3]: axes1 is axes2
Out[3]: False

All subplot parameters have to be given to the grispec_kw and subplot_kw arguments in the beginning. They can't be change easily afterwards anymore.

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

Comments

1

This feature is implemented on the master branch via PR#5146 which is targetted for mpl 2.1 which should be out in the fall.

If you need it now either run the master branch or vendor that method as a function

axarr = vendored_subplots(fig, ...)

Comments

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.