英文:
Display pyqtgraph with no margins
问题
当我显示一个带有隐藏轴的pyqtgraph时,显示的左右两侧有一些边距,顶部和底部有较小的边距:
如何去掉这些边距?contentsMargins 和 viewportMargins 都设置为零。
最小示例:
#!/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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论