Issue
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?
Solution
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
Answered By - Akshay Sehgal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.