Skip to content

Instantly share code, notes, and snippets.

@JeonghunLee
Created February 27, 2019 15:51
Show Gist options
  • Save JeonghunLee/252480befcbf05d1e7dbc09c875bb813 to your computer and use it in GitHub Desktop.
Save JeonghunLee/252480befcbf05d1e7dbc09c875bb813 to your computer and use it in GitHub Desktop.
python matplotlib의 plot 사용시 naviationtool bar 사용
## https://stackoverflow.com/questions/14896580/matplotlib-hooking-in-to-home-back-forward-button-events/15109266
## https://www.csie.ntu.edu.tw/~azarc/sna/matplotlib/lib/matplotlib/backend_bases.py
import matplotlib.pyplot as plt
from matplotlib.backend_bases import NavigationToolbar2, Event
home = NavigationToolbar2.home
back = NavigationToolbar2.back
forward = NavigationToolbar2.forward
def new_home(self, *args, **kwargs):
s = 'home_event'
event = Event(s, self)
event.foo = 100
self.canvas.callbacks.process(s, event)
home(self, *args, **kwargs)
def new_back(self, *args, **kwargs):
s = 'back_event'
event = Event(s, self)
event.foo = 100
self.canvas.callbacks.process(s, event)
back(self, *args, **kwargs)
def new_forward(self, *args, **kwargs):
s = 'forward_event'
event = Event(s, self)
event.foo = 100
self.canvas.callbacks.process(s, event)
forward(self, *args, **kwargs)
NavigationToolbar2.home = new_home
NavigationToolbar2.back = new_back
NavigationToolbar2.forward = new_forward
def handle_home(evt):
print 'new home'
print evt.foo
fig = plt.figure()
fig.canvas.mpl_connect('home_event', handle_home)
fig.canvas.mpl_connect('back_event', handle_home)
fig.canvas.mpl_connect('forward_event', handle_home)
plt.text(0.35, 0.5, 'Hello world!', dict(size=30))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment