title: Recycle a figure with Matplotlib
body: <p>I need to represent <strong>multiple datasets</strong> on the same canvas and then save each one as a figure. The original canvas is an object that contains a <strong>Matplotlib</strong> figure with several axis and subplots.</p>
  <p>In order not to create this canvas over and over again on each dataset plot, I'd like to <strong>recycle</strong> it somehow. I mean, keep it in memory and acces to it every time I want.</p>
  <p>I have tried this by using <code>copy.copy()</code> function but it doesn't work since all plots end up overlapping each other within the same figure.</p>
  <p>Here I add a <strong>Minimal Reproducible Code (MRE)</strong> to let me understand better. The canvas example figure has been made by modifying <a href="https://matplotlib.org/stable/gallery/shapes_and_collections/dolphin.html" rel="nofollow noreferrer">Matplotlib's dolphin example</a>.</p>
  <pre><code>import matplotlib.pyplot as plt
  import numpy as np
  import pandas as pd
  import copy
  
  class Canvas:
      '''Figure example to recycle'''
      def __init__(self):
          fig, ax = plt.subplots(figsize=(6, 6))
          circle = plt.Circle((0.5, 0.5), 1, facecolor='none', edgecolor='black', linewidth=3)
          ax.add_patch(circle)
          im = plt.imshow(np.random.random((100, 100)), cmap=plt.cm.plasma,
                          extent=([-0.5,1.5,-0.5,1.5]), alpha=0.6)
          im.set_clip_path(circle)
          self.fig, self.ax = fig, ax
  
  class Plot:
      '''Create a DataFrame and represent its info within a figure'''
      
      def __init__(self):
          '''x and y dataframes for xy coordinates'''
          self.x = pd.DataFrame(dict(A=np.random.random(50), B=np.random.random(50), C=np.random.random(50),
                                D=np.random.random(50)), index=np.arange(50))
          self.y = pd.DataFrame(dict(A=np.random.random(50), B=np.random.random(50), C=np.random.random(50),
                                D=np.random.random(50)), index=np.arange(50))
          self.Graph = Canvas()
          self.fig, self.ax = self.Graph.fig, self.Graph.ax
                                
      def draw(self):
          '''Plot each column of both dataframes (x and y) into the figure'''
          for i,j in zip(self.x.columns, self.y.columns):
              copy_Graph = copy.copy(self.Graph)                    
              fig, ax = copy_Graph.fig, copy_Graph.ax
              ax.scatter(self.x[i], self.y[j])
              ax.axis('off')
              fig.savefig('{}.png'.format(i))
  
  A = Plot()
  A.draw()
  </code></pre>
  <p>As can be seen in the output figures, different dataset coexist at the same figure (i.e., <code>copy()</code> function did not work), when <strong>each dataset should have one figure</strong>.</p>
  <p><a href="https://i.stack.imgur.com/9Od9f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9Od9f.png" alt="enter image description here" /></a></p>
  <p>I have also tried <code>copy.deepcopy()</code>, but the next <strong>error</strong> is constantly raised:</p>
  <blockquote>
  <p>NotImplementedError: TransformNode instances can not be copied. Consider using frozen() instead.</p>
  </blockquote>
  <p>Since the canvas creation for each plot last the 20% of the program execution, I think it's necessary to recycle the Diagram figure. Does anybody know how to achieve this?</p>
  <p>Thanks in advance!</p>