0

I have this code to plot

import matplotlib.pyplot as plt
import numpy as np

# Data
time = np.arange(0,20,0.05)
data = np.sin(time)

# Plot
main_plot  = plt.plot(time, data)
error_plot = plt.fill_between(time, data+0.5, data-0.5, alpha=0.2)

plt.show()

enter image description here

and I would like to plot, after in the code, the same plot but using the main_plot and error_plot variable. So I have this

plt.plot(*main_plot[0].get_data())
# Here I want to plot the error_plot (PolyCollection type)
# but I don't know how. Something like
# plt.plot(error_plot)

I have tried this related post with no result. I've also tried

fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_collection(error_plot)

but resulting in this error

      1 fig = plt.figure()
      2 ax = fig.add_subplot(111)
----> 3 ax.add_collection(error_plot)
      RuntimeError: Can not put single artist in more than one figure
Alejo Bernardin
  • 1,105
  • 14
  • 22
  • 2
    Matplotlib doesn't support adding the same graphical element to more than one subplot. The easiest way is to just call `ax.fill_between(...)` again (perhaps wrapping the plotting code into a function with `ax` as one of the parameters). – JohanC Dec 15 '21 at 17:52
  • Is there any technical reason to not be implemented? Or just nobody has implemented? – Alejo Bernardin Dec 16 '21 at 12:42
  • I suppose there are many technical reasons, especially when different figures might have different backends and dpi. Moreover, there is no need, as one can simply save the data and generate the plot again (saving the data needs much less memory). You might take a look at matplotlib's github issues and note that people are working very hard (during their free time). – JohanC Dec 16 '21 at 13:20

0 Answers0