Is it possible to do something like the following to modify the navigation toolbar in matplotlib?
fig = figure()
The way I found to remove unwanted toolbar items is making a subclass, which is instantiated and used in a GTK application. As I manually create Figure, FigureCanvas and NavigationToolbar objects anyway, this was the easiest way.
class NavigationToolbar(NavigationToolbar2GTKAgg):
# only display the buttons we need
toolitems = [t for t in NavigationToolbar2GTKAgg.toolitems if
t[0] in ('Home', 'Pan', 'Zoom', 'Save')]
If you want to create custom buttons, you should take a look on the definition of NavigationToolbar2
in backend_bases. You can easily add your own entries to the toolitems
list and define appropriate callback functions in your toolbar subclass.
Using PyQt5 and matplotlib version '3.0.2'
If you want to add some buttons just follow the doc given by the class NavigationToolbar2() that is initialised in NavigationToolbar2QT() wich is imported from matplotlib.backends.backend_qt5agg :
# list of toolitems to add to the toolbar, format is:
# (
# text, # the text of the button (often not visible to users)
# tooltip_text, # the tooltip shown on hover (where possible)
# image_file, # name of the image for the button (without the extension)
# name_of_method, # name of the method in NavigationToolbar2 to call
# )
So you need to redefine your class as previously said (you can also see under, the pre-defined buttons available atm). In my case I wanted to remove 2 buttons ('Save' and 'Subplots' that I commented) so that gave me :
class NavigationToolbar2QT(NavigationToolbar2QT):
# only display the buttons we need
NavigationToolbar2QT.toolitems = (
('Home', 'Reset original view', 'home', 'home'),
('Back', 'Back to previous view', 'back', 'back'),
('Forward', 'Forward to next view', 'forward', 'forward'),
(None, None, None, None),
('Pan', 'Pan axes with left mouse, zoom with right', 'move', 'pan'),
('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),
# ('Subplots', 'Configure subplots', 'subplots', 'configure_subplots'),
(None, None, None, None),
# ('Save', 'Save the figure', 'filesave', 'save_figure'),
)
And calling the NavigationToolbar2QT (still in my case) :
figure = plt.figure()
canvas = FigureCanvas(figure)
toolbar = NavigationToolbar2QT(canvas, self)
The previous answers work but are very backend-specific. A slighly more elegant solution is to subclass NavigationToolbar2, as done in this other answer: Matplotlib/Tkinter - customizing toolbar tooltips There the aim was to change the tooltips, but adding or removing a button is equally trivial.
With MPL 1.2.1 it is possible to get an handler of the navigation toolbar of a standard MPL figure through figure.canvas.toolbar
. I'm not sure about previous versions.
At least with the QT backend it is possible to add arbitrary widgets to the navigation toolbar using the QT method .addWidget()
. I suppose other backends will work using similar methods, but I haven't tested them.
Here it is a working example (using the QT backend) that adds a QLineEdit()
to the navigation toolbar to change the title of a MPL figure (run from IPython (pylab) with run -i ...
, then launch test()
):
from PySide import QtGui, QtCore
def test():
plot([1,2,3], lw=2)
q = qt4_interface(gcf())
return q # WARNING: it's paramount to return the object otherwise, with
# no references, python deletes it and the GUI doesn't respond!
class qt4_interface:
def __init__(self,fig):
self.fig = fig
toolbar = fig.canvas.toolbar
self.line_edit = QtGui.QLineEdit()
toolbar.addWidget(self.line_edit)
self.line_edit.editingFinished.connect(self.do_something)
def do_something(self, *args):
self.fig.axes[0].set_title(self.line_edit.text())
self.fig.canvas.draw()
#f = open('l','a'); f.write('yes\n'); f.flush(); f.close()
I found that just that
fig = plt.figure()
toolbar = fig.canvas.manager.toolbar
tb=toolbar.toolitems
while len(tb)>0:
tb.pop(0)
worked to remove all tools, and popping individual tools worked too. That said,
toolbar.toolitems=[]
didnt work, so the code must have another reference to this array somewhere.
In addition to torfbotl's solution above, you might have an extra button hanging at the end (the one with the green check mark).
That can be mitigated in the subclass constructor:
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
class PanOnlyToolbar(NavigationToolbar):
# only display the buttons we need
toolitems = [t for t in NavigationToolbar2GTKAgg.toolitems if
t[0] in ("Pan", )]
def __init__(self, *args, **kwargs):
super(PanOnlyToolbar, self).__init__(*args, **kwargs)
self.layout().takeAt(1) #or more than 1 if you have more buttons