“RuntimeError: Can not put single artist in more than one figure” when creating a legend


I intend to create a new legend in a separate figure for elements already plotted in another figure.

The following solution is suggested here, but it fails for PolyCollections, e.g. produced by fill_between.

<pre class="lang-py prettyprint-override">doesntwork=True import matplotlib.pyplot as plt # Here I am creating a figure and saving it to a png file fig, ax = plt.subplots() ax.plot( [0,1], [1,0], label='item 1') if doesntwork: ax.fill_between([0,1], [2,1], y2=[1.5,0.5], label='item 2') ax.legend() fig.savefig('image.png') # Here I want the legend stored in a separate png file ... # ... create a new figure for this purpose fig_leg = plt.figure(figsize=(1.5, 0.8)) ax_leg = fig_leg.add_subplot(111) # ... draw the legend ax_leg.legend(*ax.get_legend_handles_labels(), loc='center') # ... turn of the axis and save to file ax_leg.axis('off') fig_leg.savefig('legend.png')

When doing without fill_between, by setting doesntwork=False, all is fine and legend.png is produced showing the legend.

With doesntwork=True I get:

RuntimeError: Can not put single artist in more than one figure

How can I get this to work with doesntwork=True?



  • “RuntimeError: Can not put single artist in more than one figure” when creating a legend
  • Unsigned long long overflow error?
  • Redux Saga stopped by LOCATION_CHANGE too early
  • Disable on-click event for single column
  • Filtering specific lines
  • How to mock Kotlin Object in android?
  • Populating Javabean via openCSV - code explanation
  • Python Split string in a certain length
  • How to fire mouse event once for moving over child elements in Javascript?
  • .NET 4.5 MethodInfo serialization breaking change
  • Type conversion. What do I do with a PostgreSQL OID value in libpq in C?
  • Swift: How to get string values of days, months and year from a date picker?
  • Can I define models in a django project directory?
  • Run bat file in c# throws not recognized error
  • Why doesn't if(cell == nil) work?
  • How can I upload files to firebase's cloud storage with a path using the admin sdk?
  • ERROR: Could not find method armeabi-v7a() for arguments [arm64-v8a]
  • NetworkX: draw graph in layers
  • How to call a method data, only when app is in background, with a time interval of one hour
  • How to make Expiring/Signed video embed urls
  • How to plot asymnmetric errors with errorbar
  • R Multiple Regression Loop and Extract Coefficients
  • How to render a component outside datatable by ajax?
  • Clojure; select all nth element from list of lists with unequal size, for n = 1, 2,
  • Debug Azure Functions locally
  • Zip library for Windows Phone 7
  • ASP.Net MVC entity framework submit model, then open new model in edit page
  • How to HTTP POST a Blob to a different domain? [duplicate]
  • when plotting a graph using bokeh, how to remove missing date while x_axis type is datetime ,
  • How exactly do you use json_decode to pass a javascript array to php?
  • Using django-multiupload within a ModelForm
  • SELECT on JSONField with Django
  • Access to database zend framework
  • Python ctypes: Prototype with LPCSTR [out] parameter
  • How to move to lines with the same indentation in Visual Studio Code
  • Draw 9 patch dotted /dashed line on Android
  • How do I set the logging properties in a spring java configuration?
  • Unable to run testNG tests from maven
  • getting the class name of an HTML tag using XPATH
  • java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/SpringDispatcher-ser