python - Matplotlib: Plotting the same graph in two different figures without writting the "plot(x,y)" line twice -



python - Matplotlib: Plotting the same graph in two different figures without writting the "plot(x,y)" line twice -

i have simple code plots same thing in 2 different figures (fig1 , fig2). however, have write line ax?.plot(x, y) twice, 1 time ax1 , 1 time ax2. how can have 1 plot look (having multiple redondant ones source of troubles more complex code). ax1,ax2.plot(x, y) ... ?

import numpy np import matplotlib.pyplot plt #prepares info 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 same fig2 plot on fig1 ax1.plot(x, y) ax2.plot(x, y) plt.show()

you can either add together each axes list, this:

import numpy np import matplotlib.pyplot plt axes_lst = [] #prepares info 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) ax in axes_lst: ax.plot(x, y) plt.show()

or can utilize unsupported feature pull of figures in pyplot. taken http://stackoverflow.com/a/3783303/1269969

figures=[manager.canvas.figure manager in matplotlib._pylab_helpers.gcf.get_all_fig_managers()] figure in figures: figure.gca().plot(x,y)

python matplotlib

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -