0

I'm working on a python class that creates a matplotlib figure and am attempting to override some of the NavigationToolbar2 buttons (following this SO solution). I think my class is okay, however, I keep getting AttributeError. Here is my code:

import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backend_bases import NavigationToolbar2

class Display:

    def __init__(self):
        self.release_zoom = NavigationToolbar2.release_zoom
        self.fig = None

    def initialize(self):
        fig, axs = plt.subplots(2,1)
        NavigationToolbar2.release_zoom = self.new_release_zoom
        axs[0].plot([1,3,1])
        axs[1].plot([8,3,-4])
        self.fig = fig
        return self.fig, axs

    def new_release_zoom(self, *args, **kwargs):
        s = 'release_zoom_event'
        self.fig.canvas.callbacks.process(s, args[0])
        self.release_zoom(self, *args, **kwargs)

    def handle_release_zoom(self, evt):
        print('release_zoom_event')
        print(evt.xdata, evt.ydata)

disp = Display()
fig, axs = disp.initialize()
fig.canvas.mpl_connect('release_zoom_event', disp.handle_release_zoom)
plt.show()

When I use the zoom tool and zoom in on a plot, the 'release_zoom_event' message is printed, however, there is also an AttributionError.

AttributeError: 'Display' object has no attribute '_ids_zoom'

Do you see something wrong with my Display class?

Geoff D
  • 369
  • 4
  • 14
  • 1
    `self` carries to totally different meaning when used inside a class. I don't know a solution for your case, other than not putting it into a class; but a good entry point would be [Adding a Method to an Existing Object Instance](https://stackoverflow.com/questions/972/adding-a-method-to-an-existing-object-instance) – ImportanceOfBeingErnest May 09 '19 at 23:51
  • Thanks for the info and link. I think I got something working. – Geoff D May 10 '19 at 15:04

0 Answers0