簡體   English   中英

如何全屏打開matplotlib圖形?

[英]How to open matplotlib graph fullscreen?

我正在開發 pyqt5 應用程序,並且有包含用 matplotlib 制作的圖形的小部件。 我想添加一個允許用戶單擊圖形的功能,它將全屏打開。 我怎樣才能做到這一點?
小部件中的圖形是這樣構建的:

class CanvasUp(FigureCanvas):
    def __init__(self, parent=None, width=5, height=5, dpi=50):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        self.plot()

def plot(self):
            # obtaining data
            ...
            ax = self.figure.add_subplot(111)

            self.figure.text(0.5, 0.5, "TEST", transform=ax.transAxes,
                             fontsize=40, color='gray', alpha=0.5,
                             ha='center', va='center')
            ax.fill_between(x1, y1=y1, label='psavert', alpha=0.5, color='tab:green', linewidth=2)

            dt = ax.plot(x1, y1)
            ax.set_title(lab, loc='left')
            ax.grid()
            self.draw_idle()           

如果我簡化我的程序,它看起來像這樣:


from PyQt5.QtWidgets import QDialog, QVBoxLayout
from PyQt5.QtWidgets import QApplication
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import sys

class CanvasUp(FigureCanvas):
    def __init__(self, parent=None, width=5, height=5, dpi=50):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        self.plot()

    def plot(self):
        try:
            x1 = [1, 2, 3]
            y1 = [3, 2, 1]
            ax = self.figure.add_subplot(111)
            ax.set_ylim([0, max(y1)*1.15])

            self.figure.text(0.5, 0.5, "test", transform=ax.transAxes,
                             fontsize=40, color='gray', alpha=0.5,
                             ha='center', va='center')
            ax.fill_between(x1, y1=y1, label='psavert', alpha=0.5, color='tab:green', linewidth=2)
            dt = ax.plot(x1, y1)
            ax.grid()
            self.draw_idle()
        except:
            print("Bad graphs")


class MainWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.setGeometry(50, 50, 700, 700)
        layout = QVBoxLayout(self)
        plot = CanvasUp()
        layout.addWidget(plot)
        self.show()

if __name__ == '__main__':
        App = QApplication(sys.argv)
        window = MainWindow()
        sys.exit(App.exec())

如果要全屏顯示小部件,則需要將其父小部件設置為None

請注意,如果要恢復以前的狀態,還需要跟蹤布局內的先前位置。 在您的示例中並不是真正的要求,但如果有更多小部件,您必須考慮這一點。

class CanvasUp(FigureCanvas):
    toggle = pyqtSignal()
    def __init__(self, parent=None, width=5, height=5, dpi=50):
        self.fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = self.fig.add_subplot(111)
        FigureCanvas.__init__(self, self.fig)
        self.setParent(parent)
        self.plot()

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            # emit a signal to notify the parent that we want to toggle the mode
            self.toggle.emit()

    # ...

class MainWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.setGeometry(50, 50, 700, 700)
        self.setWindowTitle('fig test')
        layout = QVBoxLayout(self)
        self.plot = CanvasUp()
        layout.addWidget(self.plot)
        self.show()
        self.plot.toggle.connect(self.toggleFigure)

    def toggleFigure(self):
        if self.plot.parent():
            # store the current index in the layout
            self.layoutIndex = self.layout().indexOf(self.plot)
            self.plot.setParent(None)
            # manually reparenting a widget requires to explicitly show it,
            # usually by calling show() or setVisible(True), but this is
            # automatically done when calling showFullScreen()
            self.plot.showFullScreen()
        else:
            self.layout().insertWidget(self.layoutIndex, self.plot)

但是,如果您使用的是網格布局,則索引是不夠的,因為insertWidget僅存在於 QBoxLayouts,因此必須在重新設置窗口小部件的父級之前提取網格坐標。
考慮到這一點,雖然您可以在將小部件添加到布局時將坐標存儲在變量中,但最好僅在需要時獲取它們。

class MainWindow(QDialog):
    def __init__(self):
        super().__init__()
        self.setGeometry(50, 50, 700, 700)
        self.setWindowTitle('fig test')
        layout = QGridLayout(self)
        layout.addWidget(QPushButton(), 0, 0)
        layout.addWidget(QPushButton(), 0, 1)
        self.plot = CanvasUp()
        layout.addWidget(self.plot, 1, 0, 1, 2)
        self.show()
        self.plot.toggle.connect(self.toggleFigure)

    def toggleFigure(self):
        if self.plot.parent():
            layoutIndex = self.layout().indexOf(self.plot)
            # store the position in grid coordinates:
            # row, column, horizontal span and vertical span
            self.layoutPosition = self.layout().getItemPosition(layoutIndex)
            self.plot.setParent(None)
            self.plot.showFullScreen()
        else:
            self.layout().addWidget(self.plot, *self.layoutPosition)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM