Qt GUI中的Matplotlib小部件拒绝对齐多个轴

时间:2018-05-21 10:27:33

标签: python matplotlib pyqt

如果我在Matplotlib中绘制一些初始占位符,以便在启动时不会使我的GUI完全为空,那么尽管添加了大量的tight_layout,它仍将全部未对齐:

enter image description here

但是,如果我只调整一个像素的窗口...: enter image description here

有效!但是没有人愿意在开始工作之前告诉界面将自己拉到一起......所以有什么方法可以解决这个问题吗?虽然只是原始的Matplotlib本身似乎没有发生。

最小的UI代码:

from PyQt5.Qt import *
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np

class WidgetPlot(QWidget):
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)
        self.setLayout(QVBoxLayout())
        self.canvas = PlotCanvas(self)
        self.layout().addWidget(self.canvas)

class PlotCanvas(FigureCanvas):
    def __init__(self, parent = None, width = 1, height = 1, dpi = 100):
        self.fig = Figure(figsize = (width, height), dpi = dpi, tight_layout = True)
        self.ax1 = self.fig.add_subplot(131)
        self.ax2 = self.fig.add_subplot(132)
        self.ax3 = self.fig.add_subplot(133)

        self.axes = self.ax1, self.ax2, self.ax3

        FigureCanvas.__init__(self, self.fig)

class Ui_TraceWindow(object):
    def setupUi(self, TraceWindow):
        TraceWindow.setObjectName("TraceWindow")
        TraceWindow.resize(1000, 500)
        self.centralWidget = QWidget(TraceWindow)
        self.centralWidget.setObjectName("centralWidget")
        self.gridLayout_2 = QGridLayout(self.centralWidget)
        self.gridLayout = QGridLayout()
        self.mpl_LayoutBox = QVBoxLayout()
        self.gridLayout.addLayout(self.mpl_LayoutBox, 3, 1, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 1, 0, 1, 1)
        TraceWindow.setCentralWidget(self.centralWidget)

        # Instantiate canvas here
        self.m = WidgetPlot()
        self.canvas = self.m.canvas
        self.mpl_LayoutBox.addWidget(self.m)

        self.drawStuff()

    def drawStuff(self):
        a = np.ndarray(shape = (500, 500))

        for ax in self.canvas.axes:
            ax.clear()
            ax.imshow(a)

        self.canvas.fig.tight_layout()
        self.canvas.draw()
        self.canvas.fig.tight_layout()


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    TraceWindow = QMainWindow()
    ui = Ui_TraceWindow()
    ui.setupUi(TraceWindow)
    TraceWindow.show()
    sys.exit(app.exec_()) 

0 个答案:

没有答案