如何使用 QChart 在 QSplitter 网格中获得一些 space?

How to gain some space in a QSplitter grid with QChart?

我正试图在我制作的情节中获得一些 space。

所以在这个图中我想减少 spaces 标记的叉号。 但是我尝试的每件事都没有用。

这里是我使用的代码:

from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtChart import *
from PyQt5.QtWidgets import *
import sys
import numpy as np
from scipy import signal

def series_to_polyline(xdata, ydata):
    """Convert series data to QPolygon(F) polyline

    This code is derived from PythonQwt's function named
    `qwt.plot_curve.series_to_polyline`"""
    size = len(xdata)
    polyline = QPolygonF(size)
    pointer = polyline.data()
    dtype, tinfo = np.float, np.finfo  # integers: = np.int, np.iinfo
    pointer.setsize(2*polyline.size()*tinfo(dtype).dtype.itemsize)
    memory = np.frombuffer(pointer, dtype)
    memory[:(size-1)*2+1:2] = xdata
    memory[1:(size-1)*2+2:2] = ydata
    return polyline

class pltlfp(QMainWindow):
    def __init__(self, parent=None):
        super(pltlfp, self).__init__(parent=parent)
        self.ncurves = 0
        self.chart = QChart()
        self.chart.legend().hide()
        # self.chart.setContentsMargins(0,0,0,0)
        self.view = QChartView(self.chart)
        self.view.setRenderHint(QPainter.Antialiasing)
        self.view.setRubberBand(QChartView.RectangleRubberBand)
        # self.view.setContentsMargins(0,0,0,0)
        self.setCentralWidget(self.view)
        # self.setContentsMargins(0,0,0,0)


    def mouseDoubleClickEvent(self,event):
        if (event.button() == Qt.LeftButton):
            self.chart.zoomReset()

    def mousePressEvent(self,event):
        if (event.button() == Qt.RightButton):
            print('rigth')
        if (event.button() == Qt.LeftButton):
            print('Left')

    def set_title(self, title,xlabel=None,ylabel=None):
        self.chart.setTitle(title)
        self.chart.axisX().setTitleText(xlabel)
        self.chart.axisY().setTitleText(ylabel)

    def add_data(self, xdata, ydata, color=None):
        curve = QLineSeries()
        pen = curve.pen()
        if color is not None:
            pen.setColor(color)
        pen.setWidthF(.1)
        curve.setPen(pen)
        curve.setUseOpenGL(True)
        curve.append(series_to_polyline(xdata, ydata))
        self.chart.addSeries(curve)
        self.chart.createDefaultAxes()
        self.ncurves += 1


class SurfViewer(QMainWindow):
    def __init__(self, parent=None,sig_dict=None, t=None):
        super(SurfViewer, self).__init__()
        self.parent = parent

        self.centralWidget= QWidget()
        self.setCentralWidget(self.centralWidget)
        self.setFixedWidth(800)
        self.setFixedHeight(800)

        #tab Model selection
        self.maplt1 = pltlfp()
        self.maplt1.add_data(t,sig_dict['ydata1'], color=Qt.red)
        self.maplt1.set_title("","Time [s]","ydata1")

        self.maplt2 = pltlfp()
        self.maplt2.add_data(t,sig_dict['ydata2'], color=Qt.blue)
        self.maplt2.set_title("","Time [s]","ydata2")


        Fs=1024

        f, Pxx_den = signal.welch(sig_dict['ydata1'][Fs:],Fs, nperseg =Fs*2 )
        freq1 = f[:np.where(f>50)[0][0]]
        PSD1 = Pxx_den[:np.where(f>50)[0][0]]

        self.freq1 = pltlfp()
        self.freq1.add_data(freq1,PSD1, color=Qt.black)
        self.freq1.set_title("","Frequency [Hz]","")

        f, Pxx_den = signal.welch(sig_dict['ydata2'][Fs:],Fs, nperseg =Fs*2 )
        freq2 = f[:np.where(f>50)[0][0]]
        PSD2 = Pxx_den[:np.where(f>50)[0][0]]

        self.freq2 = pltlfp()
        self.freq2.add_data(freq2,PSD2, color=Qt.black)
        self.freq2.set_title("","Frequency [Hz]","")

        self.globallayout = QVBoxLayout()
        self.split_V = QSplitter(Qt.Vertical)

        self.split_H1 = QSplitter(Qt.Horizontal)
        self.split_H1.addWidget(self.maplt1 )
        self.split_H1.addWidget(self.freq1 )
        self.split_V.addWidget(self.split_H1)

        self.split_H2 = QSplitter(Qt.Horizontal)
        self.split_H2.addWidget(self.maplt2 )
        self.split_H2.addWidget(self.freq2 )
        self.split_V.addWidget(self.split_H2)

        self.globallayout.addWidget(self.split_V)

        self.globallayout.setContentsMargins(0,0,0,0)
        self.centralWidget.setLayout(self.globallayout)


def main():
    app = QApplication(sys.argv)
    npoints = 10*1024
    xdata1 = np.linspace(0., 1., npoints)
    ydata1 = np.sin(xdata1*2*3.14*30)
    xdata2 = xdata1
    ydata2 = np.cos(xdata2*2*3.14*30)
    sig_dict = {'ydata1':ydata1,'ydata2':ydata2 }
    ex = SurfViewer(app,sig_dict,xdata1)
    ex.setWindowTitle('window')
    ex.show()
    sys.exit(app.exec_( ))


if __name__ == '__main__':
    main()

我尝试使用 setContentsMargins(0,0,0,0)setSpacing(0),但它们对 space 的改动不大。我也尝试直接更改图中的 spaces 但它也没有用。我想要的是让地块限制真正接近 window 的边界,并且地块之间几乎没有 space。

我在这里找到了解决方案:

所以只需添加 self.chart.layout().setContentsMargins(0, 0, 0, 0)add_data 函数的末尾起作用(我没有设置正确的布局)以删除绘图之间的边距。此外,我在 __init__ 中添加 self.chart.setMargins(QMargins()) 以删除每个图形之间的 space 和每个图的限制