pyqtgraphpyqt6

Display pyqtgraph with no margins


When I display a pyqtgraph with axes hidden, there is a margin on the left and right of the display and smaller one top and bottom:

enter image description here

How can I get rid of those margins? The contentsMargins and viewportMargins are all zero.

Minimal example:

#!/usr/bin/env python

import sys
import numpy as np

from PyQt6 import QtWidgets
import pyqtgraph as pg


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.setFixedSize(600, 200)
        self.graphWidget = pg.PlotWidget()
        self.graphWidget.hideAxis("bottom")
        self.graphWidget.hideAxis("left")
        self.setCentralWidget(self.graphWidget)

        data = np.random.normal(size=1000)
        self.graphWidget.plot(data)


app = QtWidgets.QApplication(sys.argv)
main = MainWindow()
main.show()
app.exec()

Solution

  • The reason for spaces around your data is that autorange is using default padding of 0.2. Each time plot is rendered and autoranged, there is some empty space around it.

    You can set default autorange padding to 0 by using self.graphWidget.setDefaultPadding(0).

    Have a look at documentation.