0

I'm trying to reuse the Line 2D object returned by plt.plot() instead of generating the plot again.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0,50,50)
y = 2*x 
a, = plt.plot(x,y)

a is Line2D object which I'm trying to reuse it below.

<matplotlib.lines.Line2D at 0x1754aaeb1c8>

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.add_line(a)
fig

However, this results in a run time error:

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

I'm trying to find out how to reuse an object returned by plot function at other cells/other places. What's the correct way to use the Line2D object returned earlier to plot a graph without running all over again or using plt.plot(x,y) again?

Vignesh
  • 35
  • 4
  • This doesn't work because [`plt.plot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html) returns a list of [`Lines2D`](https://matplotlib.org/stable/api/_as_gen/matplotlib.lines.Line2D.html#matplotlib.lines.Line2D); the plotted lines. In order to reuse a plot, you must use an [`axes`](https://matplotlib.org/stable/api/axes_api.html). This is different than `seaborn` (which us an API for `matplotlib`), where `axes` level plots return an `Axes`, so you can do `p = sns.boxplot(...)` – Trenton McKinney Sep 18 '21 at 21:37
  • `a` in your question not the same as `a = Line2D(x,y)` because `a` in your question has already been plotted to a figure, while `a` in the answer, has not. [Artist Tutorial](https://matplotlib.org/stable/tutorials/intermediate/artists.html) – Trenton McKinney Sep 18 '21 at 21:46
  • Also see the function in the class [`def set_figure(self, fig):`](https://matplotlib.org/stable/_modules/matplotlib/artist.html#Artist.set_figure) – Trenton McKinney Sep 18 '21 at 22:06
  • Ah, that clarifies a lot. Is there any use of the ` a ` that is returned in my code? – Vignesh Sep 19 '21 at 03:59
  • plot artists have many properties, as you can see by clicking the `Lines2D` link in the first comment. Some of those can be altered after the plot is created, before showing the figure. Like changing the color as shown in this [answer](https://stackoverflow.com/a/41709394/7758804) – Trenton McKinney Sep 19 '21 at 04:03

1 Answers1

0

You can directly create a Line2D object and then resuse it as many times.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D

x = np.linspace(0,50,50)
y = 2*x 
a = Line2D(x,y)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.add_line(a)
fig

enter image description here

Akshay Sehgal
  • 18,741
  • 3
  • 21
  • 51
  • Why does the code for mine doesn't work? I'm adding Line2D object to an axes as well but it raises an "Can not put single artist in more than one figure" error. Also, trying to reuse the line2d object from your code onto a new figure throws me the same error. ` fig1 = plt.figure() ax1 = fig1.add_subplot(111) ax1.add_line(a) fig1 ` Does this occur because the first object is already linked to a figure? Is there any way to reuse the line2d object onto a different figure? – Vignesh Sep 18 '21 at 20:51