I'm just starting using Matplotlib the "right" way. I'm writing various programs that will each give me back a time series, and I'm looking to superimpose the graphs of the various time series, like this:
I think what I want is a single Axes
instance defined in the main function, then I call each of my little functions, and they all return a Line2D
instance, and then I'll put them all on the Axes
object I created.
But I'm having trouble taking an existing Line2D
object and adding it to an existing Axes
object (like I'd want to do with the output of my function.) I thought of taking a Line2D
called a
and say ax.add_line(a)
.
import matplotlib.pyplot as plt
a, = plt.plot([1,2,3], [3,4,5], label = 'a')
fig, ax = plt.subplots()
ax.add_line(a)
Gives me a RuntimeError: "Can not put single artist in more than one figure."
I'm guessing that over time Matplotlib has stopped wanting users to be able to add a given line to any Axes
they want. A similar thing is discussed in the comments of this answer, except there they're talking about an Axes
object in two different Figure
objects.
What's the best way to accomplish what I want? I'd rather keep my main script tidy, and not say ax.plot(some_data)
over and over when I want to superimpose these lines.