显示没有边距的pyqtgraph。

huangapple go评论56阅读模式
英文:

Display pyqtgraph with no margins

问题

当我显示一个带有隐藏轴的pyqtgraph时,显示的左右两侧有一些边距,顶部和底部有较小的边距:

如何去掉这些边距?contentsMarginsviewportMargins 都设置为零。

最小示例:

#!/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()
英文:

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:

显示没有边距的pyqtgraph。

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

答案1

得分: 0

您数据周围有空格的原因是autorange使用默认的填充值0.2。每次渲染图形并进行autorange时,周围都会有一些空白空间。

您可以通过使用self.graphWidget.setDefaultPadding(0)来将默认的autorange填充值设置为0。

请查看文档

英文:

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.

huangapple
  • 本文由 发表于 2023年6月19日 17:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定