使用 PyQt5 和 matplotlib 版本 '3.0.2'
如果您想添加一些按钮,只需按照从 matplotlib.backends.backend_qt5agg 导入的 NavigationToolbar2QT() 中初始化的类 NavigationToolbar2() 给出的文档:
# 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
# )
因此,您需要重新定义您的类,如前所述(您还可以在下面看到可用的预定义按钮)。就我而言,我想删除 2 个按钮(我评论的“保存”和“子图”),这样我就得到了:
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'),
)
并调用 NavigationToolbar2QT(在我的情况下):
figure = plt.figure()
canvas = FigureCanvas(figure)
toolbar = NavigationToolbar2QT(canvas, self)