简体   繁体   中英

matplotlib - Figure object has no attribute 'plot'. Reference problems

I know there are other people asking this question in their specific context, but none of those answers have gotten me closer to figuring this out. I am trying to use a class in matplotlib to add plots to a figure.

I'm trying to get the class to take as an argument a Line2D object and interpret it a bit - this is working. The class also registers a callback to mouse click events on the plot window - this is also working. Here's what's not working:

fig, ax = plt.subplots()
series, = ax.plot(x_data, y_data)
classthing = MyClass(series)
plt.show()

And here is the class that is being instanced:

class MyClass:
    def __init__(self, series):
        self.series = series
        self.cid = series.figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        self.series.figure.plot([xdata], [ydata])  
        self.series.figure.canvas.draw()

On mouse click I want to add a plot to the figure that I clicked on. This is really similar to the example here: https://matplotlib.org/users/event_handling.html

This code successfully creates the plot and the object, but obviously I'm not calling the plot() method from the correct reference since I get the title error on the second to last line. How can I reference the plot object properly given only the Line2D object that is in it? I am not anywhere near competent with OOP.

Thanks in advance for your help.

You can't both plot an ax and use it again. The series in your code is a Line2D Object, which doesn't have an attribute plot .

What you can do is to add a ax object to your Class like this:

class MyClass:
    def __init__(self, series, fig):
        self.series = series
        self.figure = fig
        self.cid = self.figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        self.series.plot([xdata], [ydata])  
        self.figure.canvas.draw()

fig, ax = plt.subplots()
series = ax
classthing = MyClass(series)
classthing(some_event) # call your class

Now since you're passing an ax to the class your call will be successful, you still need to pass xdata and ydata to your call, though.

Since you'll be calling figure due to canvas draws, you'll need to pass that into your object as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM