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

pyqtgraph OpenGL 3D散点图报错:'list'对象无'size'属性

pyqtgraph OpenGL 3D散点图报错:'list'对象无'size'属性的解决方法

报错原因

从报错追踪信息可以看到,GLScatterPlotItem.py第152行尝试访问pos.size和pos.shape属性,但传入的pos是普通Python列表——这两个属性是numpy数组特有的。问题出在代码中传给GLScatterPlotItem的pos参数使用了Python列表,而非numpy数组,导致OpenGL绘制时无法正确解析坐标数据,同时出现空白绘图和AttributeError。

解决方案

将所有传给pos参数的Python列表转换为numpy数组,且坐标数据需为N×3的二维数组(N为点的数量,每个点对应一行三维坐标),即使单个点也要封装成二维数组形式。

修改步骤

  1. 初始化GLScatterPlotItem时,将坐标列表转为numpy二维数组:
pts = np.array([[obj.x, obj.y, obj.z]])  # 单个点的二维数组格式
  1. 在update方法中,同样将更新后的坐标列表转为numpy二维数组:
pts = np.array([[obj.x + obj.dx, obj.y + obj.dy, obj.z + obj.dz]])

修改后的完整代码

# -*- coding: utf-8 -*-

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.opengl as gl
import pyqtgraph as pg
import numpy as np
import sys
import time
import random


class Visualizer(object):
    def __init__(self):
        self.traces = dict()
        self.app = QtGui.QApplication(sys.argv)
        self.w = gl.GLViewWidget()
        self.w.opts['distance'] = 40
        self.w.setWindowTitle('pyqtgraph example: GLLinePlotItem')
        self.w.setGeometry(0, 110, 1920, 1080)
        self.w.show()
        
        self.listPoints = []
        self.listPoints.append(self.PointsObj(0))
        self.listPoints.append(self.PointsObj(0))
        self.listPoints.append(self.PointsObj(0))
            
        for idObj, obj in enumerate(self.listPoints):
            # 修改:将列表转为numpy二维数组
            pts = np.array([[obj.x, obj.y, obj.z]]) 
            print(pts)
            self.traces[idObj] = gl.GLScatterPlotItem(
                pos=pts,
                color=pg.glColor((0, 6.5)),
                size=3,
                pxMode=True
            )
            self.w.addItem(self.traces[idObj])
        
            
    class PointsObj:
        def __init__(self, type):
            self.type= type
            self.x= random.randint(-10, 10)
            self.y= random.randint(-10, 10)
            self.z= random.randint(-10, 10)
            self.dx= 0
            self.dy= 0
            self.dz= 0
            

    def start(self):
        if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
            QtGui.QApplication.instance().exec_()

    def set_plotdata(self, name, points, color, size):
        self.traces[name].setData(pos=points, color=color, size=size)

    def update(self):
        stime = time.time()
        for idObj, obj in enumerate(self.listPoints):
            # 修改:将列表转为numpy二维数组
            pts = np.array([[obj.x + obj.dx, obj.y + obj.dy, obj.z + obj.dz]])
            self.set_plotdata(
                name=idObj, points=pts,
                color=pg.glColor((0, 6.5)),
                size=3
            )
        
    def animation(self):
        timer = QtCore.QTimer()
        timer.timeout.connect(self.update)
        timer.start(10)
        self.start()


# Start event loop.
if __name__ == '__main__':
    v = Visualizer()
    v.animation()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 07:15:42