How to disable the maximize, minimize and close button of the matplotlib window?

前端 未结 1 1206
攒了一身酷
攒了一身酷 2021-01-29 13:31

I\'m plotting a figure with matplotlib and I would like to disable the maximize, minimize and close button of the window.

Is it possbile?

1条回答
  •  深忆病人
    2021-01-29 13:47

    The matplotlib window is created by a backend in use. Different backends require different solutions. A common backend is the Qt backend, so the following shows two solution for this backend.

    Embedding into PyQt

    You can embed the matplotlib plot into a custom GUI, e.g. using PyQt. Then you have all the options the GUI package provides to adjust the window.

    In PyQt the window's top bar can be turned off via setting QtCore.Qt.CustomizeWindowHint as window flag

    QMainWindow.setWindowFlags( QtCore.Qt.CustomizeWindowHint )
    

    Left is the normal version, right is the version with the custom WindowFlag.

    Complete example:

    import matplotlib.pyplot as plt
    from PyQt4 import QtGui, QtCore
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
    
    class Window(QtGui.QMainWindow):
        def __init__(self, fig):
            self.qapp = QtGui.QApplication([])
    
            QtGui.QMainWindow.__init__(self)
            self.setWindowFlags( QtCore.Qt.CustomizeWindowHint )
            self.widget = QtGui.QWidget()
            self.setCentralWidget(self.widget)
            self.widget.setLayout(QtGui.QVBoxLayout())
            self.widget.layout().setContentsMargins(0,0,0,0)
            self.widget.layout().setSpacing(0)
    
            self.fig = fig
            self.canvas = FigureCanvas(self.fig)
            self.canvas.draw()
    
            self.nav = NavigationToolbar(self.canvas, self.widget)
            self.widget.layout().addWidget(self.nav)
            self.widget.layout().addWidget(self.canvas)
    
            self.show()
            exit(self.qapp.exec_()) 
    
    
    # create a figure and some subplots
    fig, ax = plt.subplots(figsize=(2.4,3))
    ax.plot([2,3,5,1])
    fig.tight_layout()
    
    # pass the figure to the custom window
    a = Window(fig)
    

    Another option is to just hide the minimize and maximumize buttons, but not the top bar.

    QMainWindow.setWindowFlags( QtCore.Qt.WindowCloseButtonHint )
    

    Obtaining canvas MainWindow

    If you don't want to create a complete window yourself you may obtain the window through the canvas' parent. Then the same options as above apply. In order for this to work you would need to make sure you are actually using the backend that you later want to make changes to.

    import matplotlib
    # make sure Qt backend is used
    matplotlib.use("Qt4Agg")
    from PyQt4 import QtCore  
    import matplotlib.pyplot as plt
    
    # create a figure and some subplots
    fig, ax = plt.subplots(figsize=(2.4,3))
    ax.plot([2,3,5,1])
    fig.tight_layout()
    
    
    plt.gcf().canvas.parent().setWindowFlags( QtCore.Qt.CustomizeWindowHint )
    plt.show()
    

    0 讨论(0)
提交回复
热议问题