Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove toolbar buttons from matplotlib

I want to remove some of the buttons from plot toolbar (matplotlib).

I saw that there are some old solutions:

How to modify the navigation toolbar easily in a matplotlib figure window?

But all the answers uses GUI frameworks (QT, TKinter).

Is there a new solution which doesn't use GUI frameworks ?

enter image description here

like image 993
user3668129 Avatar asked Apr 21 '19 04:04

user3668129


People also ask

How do I hide the toolbar in Matplotlib?

Make sure to call mpl. rcParams['toolbar'] = 'None' before you instantiate any figures.

How to make matplotlib zoomable?

Press the right mouse button to zoom, dragging it to a new position. The x axis will be zoomed in proportionately to the rightward movement and zoomed out proportionately to the leftward movement. The same is true for the y axis and up/down motions.


1 Answers

You can do it by adding following lines of code before creating a plot object:

import matplotlib as mpl
mpl.rcParams['toolbar'] = 'None' 

If you want to delete some buttons selectively, you need to redefine the toolitems variable instead:

from matplotlib import backend_bases
# mpl.rcParams['toolbar'] = 'None'
backend_bases.NavigationToolbar2.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),
        ('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),
        (None, None, None, None),
        ('Save', 'Save the figure', 'filesave', 'save_figure'),
      )

I have removed two lines from the original variable mpl.backend_bases.NavigationToolbar2.toolitems which normally reads:

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'),
  )

EDIT

I have realized that it works with backend 'TkAgg'. For the backend 'Qt5Agg' we need to do some additional monkey patching just after modifying toolitems. Namely:

if matplotlib.get_backend() == 'Qt5Agg':
    from matplotlib.backends.backend_qt5 import NavigationToolbar2QT
    def _update_buttons_checked(self):
        # sync button checkstates to match active mode (patched)
        if 'pan' in self._actions:
            self._actions['pan'].setChecked(self._active == 'PAN')
        if 'zoom' in self._actions:
            self._actions['zoom'].setChecked(self._active == 'ZOOM')
    NavigationToolbar2QT._update_buttons_checked = _update_buttons_checked
like image 193
freude Avatar answered Sep 19 '22 02:09

freude