如何从 matplotlib 中删除工具栏按钮 [英] How to remove toolbar buttons from matplotlib

查看:87
本文介绍了如何从 matplotlib 中删除工具栏按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从绘图工具栏(matplotlib)中删除一些按钮.

我看到有一些旧的解决方案:

<块引用>

解决方案

您可以通过在创建绘图对象之前添加以下代码行来实现:

 将matplotlib导入为mplmpl.rcParams ['toolbar'] ='无'

如果要选择性地删除一些按钮,则需要重新定义toolitems变量:

from matplotlib import backend_bases# mpl.rcParams['toolbar'] = 'None'backend_bases.NavigationToolbar2.toolitems = (('Home', '重置原始视图', 'home', 'home'),(返回",返回上一个视图",返回",返回"),(转发",转发到下一个视图",转发",转发"),(无,无,无,无),('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),(无,无,无,无),('Save', 'Save the figure', 'filesave', 'save_figure'),)

我已经从原始变量 mpl.backend_bases.NavigationToolbar2.toolitems 中删除了两行,它通常为:

toolitems = (('Home', '重置原始视图', 'home', 'home'),('返回', '返回上一个视图', '返回', '返回'),('转发', '转发到下一个视图', '转发', '转发'),(无,无,无,无),(平移",左鼠标平移轴,向右缩放",移动",平移"),('Zoom', 'Zoom to rectangle', 'zoom_to_rect', 'zoom'),('子图', '配置子图', '子图', 'configure_subplots'),(无,无,无,无),('Save', 'Save the figure', 'filesave', 'save_figure'),)

编辑

我意识到它可以与后端TkAgg"配合使用.对于后端'Qt5Agg',我们需要在修改toolitems 之后做一些额外的猴子补丁.即:

如果 matplotlib.get_backend() == 'Qt5Agg':从matplotlib.backends.backend_qt5导入NavigationToolbar2QTdef _update_buttons_checked(self):#同步按钮的检查状态以匹配活动模式(已修补)如果在 self._actions 中平移":self._actions['pan'].setChecked(self._active == 'PAN')如果在 self._actions 中放大":self._actions['zoom'].setChecked(self._active == 'ZOOM')NavigationToolbar2QT._update_buttons_checked = _update_buttons_checked

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 ?

解决方案

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

这篇关于如何从 matplotlib 中删除工具栏按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆