Adding An Item In Matplotlib´s Toolbar
Solution 1:
Based on this related SO answer, I wanted a way that doesn't involve extending any class and is much less verbose (pretty much 3 lines). Taking a look to this sources and using fig.canvas.toolbar
I made this snippet work:
It plots an image and adds two buttons to the toolbar: previous and next, which can be bound to any parameter-less function. This can be very convenient, for instance, for scrolling through the images on a folder and drawing some points or lines on them using scatter
, as explained in this other SO post. Let me know how it works for you!
fig, ax = plt.subplots()
fig.canvas.manager.toolbar._Button("PREVIOUS", "back_large", <ACTION_PREV>)
fig.canvas.manager.toolbar._Button("NEXT", "forward_large", <ACTION_NEXT>)
im = plt.imread("hello.png")
implot = ax.imshow(im)
fig.show()
Note the bigger arrows that were added to the toolbar.
The drawback of this method is that it uses the protected
_Button
method, whose interface may change in further versions, but this way seems fine to me for some casual scripting.- Also note that the method signature (see the sources) requires a path to an image that (in my case) has to be found at
/usr/local/lib/python2.7/dist-packages/matplotlib/mpl-data/images
. Again, there is probably a way to overcome this but probably more verbose than the inheriting alternative.
Cheers, Andres
BONUS:
classImageViewer(object):
"""Given a path to a directory, this class opens a matplotib window with two
custom buttons that allow scrolling through the images in the directory.
Usage example: iv = ImageViewer("/home/pau/Images")
"""def__init__(self, seq_path, img_extension=".png"):
self.ids = [join(seq_path, splitext(img)[0]) for img in listdir(seq_path)
if img.endswith(img_extension)]
self.mod = len(self.ids)
self.idx = 0
self.fig, self.ax = plt.subplots()
self.fig.canvas.manager.toolbar._Button("PREVIOUS", "back_large", self.prev)
self.fig.canvas.manager.toolbar._Button("NEXT", "forward_large", self.next)
self._plot(0)
def_plot(self, idx):
im = plt.imread(self.ids[idx]+".png")
implot = self.ax.imshow(im, cmap='Greys_r', interpolation='nearest')
self.fig.show()
defnext(self):
self.idx = (self.idx+1)%self.mod
self._plot(self.idx)
defprev(self):
self.idx = (self.idx-1)%self.mod
self._plot(self.idx)
Solution 2:
I have solved the problem. I´ ve found this code:
Matplotlib/Tkinter - customizing toolbar tooltips (broken link)
So, I created a subclass and added as it says in the link. This is the code:
classMyToolbar(NavigationToolbar2):
def__init__(self, figure_canvas, parent= None):
self.toolitems = (('Home', 'Lorem ipsum dolor sit amet', 'home', 'home'),
('Back', 'consectetuer adipiscing elit', 'back', 'back'),
('Forward', 'sed diam nonummy nibh euismod', 'forward', 'forward'),
(None, None, None, None),
('Pan', 'tincidunt ut laoreet', 'move', 'pan'),
('Zoom', 'dolore magna aliquam', 'zoom_to_rect', 'zoom'),
(None, None, None, None),
('Subplots', 'putamus parum claram', 'subplots', 'configure_subplots'),
('Save', 'sollemnes in futurum', 'filesave', 'save_figure'),
('Port', 'Select', "select", 'select_tool'),
)
NavigationToolbar2.__init__(self, figure_canvas, parent= None)
defselect_tool(self):
print"You clicked the selection tool"
And the, you can add this toolbar by writing:
self.navigation_toolbar = MyToolbar(self.figure_canvas, self)
self.navigation_toolbar.update()
If you want to only let your own button, you must erase all the others items from self.toolitems
. For example:
self.toolitems = (
('Port', 'Select', "select", 'select_tool'),
)
With this, you will only see your own button in the NavigationToolbar
Hope this helps.
Post a Comment for "Adding An Item In Matplotlib´s Toolbar"