Plot Something In One Figure, And Use It Again Later For Another Figure
I hope I'm asking this question at the right place. I have a for-loop, in that many figures are created. After the loop is finished I want to produce one more figure with three of
Solution 1:
In matplotlib an axes (a subplot) is always part of exactly one figure. While there are options to copy an axes from one figure to another, it is a rather complicated procedure. Instead, you may simply recreate the plot as often as you need it in several figures. Using a function, which takes the axes to plot to as argument, makes this rather straight-forward.
In order to save all three figures in a pdf, you may use pdfPages
as shown in the bottom of the code.
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t)*np.cos(2*np.pi*t)+(t/10)**2.
t1=np.arange(0.0,5.0,0.1)
t2=np.arange(0.0,5.0,0.02)
def plot(ax, i):
ax.set_title('Jon Snow')
kraft_plot,=ax.plot(t1,np.sin(t1),color='purple')
tyrion=ax.axvline(2,color='darkgreen',ls='dashed')
ax.set_ylabel('Kraft [N]',color='purple',fontweight='bold')
ax.set_xlabel('Zeit [s]',fontweight='bold')
ax2=ax.twinx()
strecke_plot,=ax2.plot(t2,t2/5,color='grey',label='Verlauf der Strecke')
ax2.set_ylabel('Strecke [mm]',color='grey',fontweight='bold')
ax.legend((kraft_plot,tyrion,strecke_plot),('Jonny','Dwarf','andalltherest'),loc=2)
figures=[]
for i in range(2):
fig= plt.figure(i)
ax1=fig.add_subplot(111)
plot(ax1, i)
figures.append(fig)
# create third figure
fig, (ax1,ax2) = plt.subplots(nrows=2)
plot(ax1, 0)
plot(ax2, 1)
figures.append(fig)
from matplotlib.backends.backend_pdf import PdfPages
with PdfPages('multipage_pdf.pdf') as pdf:
for fig in figures:
pdf.savefig(fig)
plt.show()
Three page pdf output:
Post a Comment for "Plot Something In One Figure, And Use It Again Later For Another Figure"