Making copies of figures and selectively writing them to a multipage PDF


I am attempting to build a set of Figure objects (based on
matplotlib.figure) which are essentially the same chart with different
x-axis limits. I would like to do this in a function that returns the
figures in a list so that i can choose to write some or all of the
figures to a multipage PDF at a later date.

I attempted to do the following:



import matplotlib.figure as fig
from matplotlib.backends.backend_pdf import PdfPages, FigureCanvasPdf

xdata = [1,2,3,4,5]
ydata = [5,4,3,2,1]

fig1 = fig.Figure(figsize=(2,2))

ax1 = fig.Figure.add_subplot(fig1,111)


figures = []



pdf = PdfPages('output.pdf')

for figure in figures:
       canvas = FigureCanvasPdf(figure)
       figure.savefig(pdf, format="pdf")



In the actual code the list of figures is returned from a function to
another part of the code, but this is roughly equivalent. I realise in
this example i can just write directly to the pdf file after appending
fig1, but in the actual code i do not wish to write the data to a file
until later on.

The output i get from this code is two identical pages of PDF where
the x limits are set to 3,5. I understand this is because I am simply
making two identical references to the figure object in the list and
by modifying fig1 i also affect the fig1 that is 'stored' in the first
position of the array. My question is how do I avoid this? I have
tried to use copy.copy() and copy.deepcopy(). The former does not work
correctly (for fairly obvious reasons) and the latter does not work
with the figure object giving me an error: "NotImplementedError:
TransformNode instances can not be copied. Consider using frozen()

So, how can i go about making a list of 'charts' and then output them
selectively to a PDF at a later time?

Many thanks and apologies for any information I have left out.


P.S. as an aside, can anyone confirm whether it is possible to write a
multipage PDF using the cairo backend?