You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQtGraph代码PyQt5运行正常,PySide2下绘图超出区域求助

Solution for PySide2 + PyQtGraph Line Overflow Issue

Let's break down the problems in your code and fix them step by step:

1. Resolve Conflicting Central Widget Setup

In your MainWindow.__init__ method, you're creating a new PlotWidget and setting it as the central widget, then immediately overwriting this with the setupUi call (which sets up the central widget from your Qt Designer UI). This creates a layout conflict that behaves differently across PyQt5 and PySide2.

Remove these two lines entirely:

self.graphWidget = pg.PlotWidget()
self.setCentralWidget(self.graphWidget)

2. Regenerate UI Code for PySide2 Compatibility

You used pyuic5 to generate UI code tailored for PyQt5. For PySide2, you need to regenerate the UI file using the PySide2-specific tool. Run this command in your terminal:

pyside2-uic your_ui_file.ui -o qtGraphTestPySide2.py

Then update your main code to import from this new PySide2-compatible UI file:

from qtGraphTestPySide2 import Ui_MainWindow

3. Auto-Adjust Plot Range to Fit Data

After adding your curve item, explicitly tell the PlotWidget to adjust its view range to match your data. This ensures your line stays within the visible plot area. Update your slot1 method:

def slot1(self):
    print("slot1")
    # plot data: x, y values
    crvItem = pg.PlotCurveItem()
    self.umw.graphicsView.addItem(crvItem)
    # plot y = x^2
    crvItem.xData = np.array(range(100))*0.1
    crvItem.yData = crvItem.xData**2
    # Force the plot to fit the data bounds
    self.umw.graphicsView.autoRange()

4. Full Updated Main Code (PySide2 Version)

from PySide2 import QtWidgets, QtCore
from pyqtgraph import PlotWidget, plot
import pyqtgraph as pg
from qtGraphTestPySide2 import Ui_MainWindow
import sys
import numpy as np

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.umw = Ui_MainWindow()
        self.umw.setupUi(self)

    def slot1(self):
        print("slot1")
        crvItem = pg.PlotCurveItem()
        self.umw.graphicsView.addItem(crvItem)
        crvItem.xData = np.array(range(100))*0.1
        crvItem.yData = crvItem.xData**2
        # Auto-fit the plot to the curve data
        self.umw.graphicsView.autoRange()

def main():
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Why This Works:

  • The conflicting central widget setup caused layout inconsistencies that PyQt5 handled more leniently, but PySide2 enforces stricter layout rules. Removing the redundant widget fixes this.
  • Regenerating the UI code with pyside2-uic ensures all Qt imports and widget initializations are fully compatible with PySide2.
  • Calling autoRange() explicitly tells PyQtGraph to adjust the plot's view bounds to match your data, preventing the line from overflowing the visible area.

内容的提问来源于stack exchange,提问作者ytakuya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:49:31