3

I have this simple code that plots exactly the same thing in two different figures (fig1 and fig2). However, I have to write the line ax?.plot(x, y) twice, once for ax1 and once for ax2. How can I have only one plot expression (having multiple redondant ones could be a source of troubles for my more complex code). Something like ax1,ax2.plot(x, y) ... ?

import numpy as np
import matplotlib.pyplot as plt

#Prepares the data
x = np.arange(5)
y = np.exp(x)

#plot fig1
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)

#plot fig2
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)

#adds the same fig2 plot on fig1
ax1.plot(x, y)
ax2.plot(x, y)

plt.show()
user2076688
  • 245
  • 3
  • 12

2 Answers2

1

You can either add each axes to a list, like this:

import numpy as np
import matplotlib.pyplot as plt

axes_lst = []    
#Prepares the data
x = np.arange(5)
y = np.exp(x)


#plot fig1
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
axes_lst.append(ax1)

#plot fig2
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
axes_lst.append(ax2)

for ax in axes_lst:
    ax.plot(x, y)

plt.show()

or you can use this unsupported feature to pull all of the figures in pyplot. Taken from https://stackoverflow.com/a/3783303/1269969

figures=[manager.canvas.figure
         for manager in matplotlib._pylab_helpers.Gcf.get_all_fig_managers()]
for figure in figures:
    figure.gca().plot(x,y)
Community
  • 1
  • 1
placeybordeaux
  • 2,138
  • 1
  • 20
  • 42
  • I had no idea I could do that. Thanks a lot! – user2076688 Feb 15 '13 at 20:10
  • If you are talking about the second method, remember that there is no garuntee that this will work on versions going forward. the __pylab_helpers indicate that it is supposed to be private, so you can't rely on it working all of the time. – placeybordeaux Feb 15 '13 at 20:29
  • I would _strongly_ suggest that you chose different names for you lists and loop variables that do not shadow `pyplot` functions. Further, your second example does not work, you need to do `figure.gca().plot(x, y)` and will plot into _every_ open figure, not just the two you might want. – tacaswell Feb 15 '13 at 21:01
  • Why do you suggest I change the list and loop variables? They are in a different namespace than pyplot, are you just worried about confusion? I have edited the second one. I agree the second one should probably not be used, but I thought I would include it as I found it interesting that it exists. – placeybordeaux Feb 15 '13 at 21:10
  • because alot if people (my self included) work in `ipython --pylab` which imports everything from `pyplot` so while you are technically correct, this could mess some people up. Also, it's just confusing. Use `fig` and `figs` instead, less typing, less chance of a newbie copy-pasting your code and ruining their interactive session, and no chance of confusion. – tacaswell Feb 15 '13 at 21:29
  • Fair enough. I personally don't feel the need to use short variables names due to time spent typing, but I didn't know about the `ipython --pylab` bit. good to know. – placeybordeaux Feb 15 '13 at 21:31
  • If you have not tried it, I highly recommend it, it makes life very easy, and provides an interface that is comfortable to matlab refugees (it also imports a big swath of `numpy`). – tacaswell Feb 15 '13 at 21:34
1

Without knowing about matplotlib, you could add all your axes (?) to a list:

to_plot = []
to_plot.append(ax1)
...
to_plot.append(ax2)
...

# apply the same action to each ax
for ax in to_plot: 
    ax.plot(x, y)

You could then add as many as you like, and the same thing will happen to each.

wrgrs
  • 2,467
  • 1
  • 19
  • 24