FigureCanvas 的缩放高度与应用程序窗口大小?

问题描述 投票:0回答:0

所以我正在用 PyQt 构建一个 GUI。我的 QVBoxLayout 中的其中一行是我用于绘图的 FigureCanvas。我不知道如何使用应用程序窗口缩放画布的高度。这两个屏幕截图显示了即使我最大化了应用程序窗口,画布高度也没有真正改变。下面是 FigureCanvas 小部件的代码。有人知道我做错了什么吗?我试过 setSizePolicy、setContentsMargins、stretches,但我认为我对它们的理解不足以正确使用它们。

class InteractivePlotWidget(QWidget):
    def __init__(self, parent=None):
        super(InteractivePlotWidget, self).__init__(parent)

        # Create a Figure and a FigureCanvas
        self.fig, self.ax = plt.subplots(
            tight_layout=True,
            facecolor='none',edgecolor='k'
        )
        self.canvas = FigureCanvas(self.fig)
        self.canvas.setContentsMargins(0, 0, 0, 0);
        self.canvas.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        self.canvas.setStyleSheet("background-color:transparent;")
        self.canvas.draw()

        # Create a NavigationToolbar for interactivity
        self.toolbar = NavigationToolbar(self.canvas, self)

        # Create a layout and add the canvas and toolbar to it
        self.layout = QVBoxLayout(self)
        self.layout.addWidget(self.toolbar)
        self.layout.addWidget(self.canvas)
        self.layout.setContentsMargins(0, 0, 0, 0);
    
        # initial properties
        self.ax.set_axis_off()
        self.ax.set_facecolor('none')
    
        # init
        self.image = None
        self.puzzle = None

    def update_plot(self, image, alpha, bound_color):
        # Create a simple example plot
        self.ax.clear()
        self.image = self.ax.imshow(image, alpha=alpha)
        self.ax.set_xlim([0,image.width])
        self.ax.set_ylim([0,image.height])
        self.ax.set_axis_off()
        self.ax.set_facecolor('none')
        self.ax.axis("tight")
        self.ax.set_aspect('equal')
        # Draw the plot on the canvas
        self.canvas.draw()

python qt matplotlib pyqt
© www.soinside.com 2019 - 2024. All rights reserved.