0

I'm trying to gain some space aroung plot I made.

enter image description here

So in this figure I would like reduce spaces marked by a cross. But every thing I tried didn't work.

Here the code I used:

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()

I tried to use setContentsMargins(0,0,0,0) and setSpacing(0) but they didn't change the space that much. I tried also to change the spaces in the plot directly but It didn't work neither. What I would like is to have plot limit really close to the border of the window and almost no space between plots.

ymmx
  • 4,769
  • 5
  • 32
  • 64

1 Answers1

0

I found a solution here : How to remove margin from QChartView or QChart

So just adding self.chart.layout().setContentsMargins(0, 0, 0, 0) at the end of the add_data function works (I wasn't setting the rigth layout) to remove margins between plots. Also I add self.chart.setMargins(QMargins()) in the __init__ in order to remove space between each graph and the limits of each plots

ymmx
  • 4,769
  • 5
  • 32
  • 64