gpt4 book ai didi

python - 绘制项目时出错

转载 作者:行者123 更新时间:2023-12-05 05:35:26 27 4
gpt4 key购买 nike

我在使用 pyqtgraph 时遇到错误。我正在尝试使用 OpenGL 绘制 3D 散点图。然而,当我运行我的代码时,我得到一个空白图和这个输出:

|==============================>>
| Traceback (most recent call last):
| File "C:\Users\T\anaconda3\lib\runpy.py", line 197, in _run_module_as_main
| return _run_code(code, main_globals, None,
| File "C:\Users\T\anaconda3\lib\runpy.py", line 87, in _run_code
| exec(code, run_globals)
| File "C:\Users\T\Anaconda3\Lib\site-packages\spyder_kernels\console\__main__.py", line 23, in <module>
| start.main()
| File "C:\Users\T\Anaconda3\Lib\site-packages\spyder_kernels\console\start.py", line 328, in main
| kernel.start()
| File "C:\Users\T\Anaconda3\Lib\site-packages\ipykernel\kernelapp.py", line 677, in start
| self.io_loop.start()
| File "C:\Users\T\Anaconda3\Lib\site-packages\tornado\platform\asyncio.py", line 199, in start
| self.asyncio_loop.run_forever()
| File "C:\Users\T\anaconda3\lib\asyncio\base_events.py", line 601, in run_forever
| self._run_once()
| File "C:\Users\T\anaconda3\lib\asyncio\base_events.py", line 1905, in _run_once
| handle._run()
| File "C:\Users\T\anaconda3\lib\asyncio\events.py", line 80, in _run
| self._context.run(self._callback, *self._args)
| File "C:\Users\T\Anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 471, in dispatch_queue
| await self.process_one()
| File "C:\Users\T\Anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 460, in process_one
| await dispatch(*args)
| File "C:\Users\T\Anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 367, in dispatch_shell
| await result
| File "C:\Users\T\Anaconda3\Lib\site-packages\ipykernel\kernelbase.py", line 662, in execute_request
| reply_content = await reply_content
| File "C:\Users\T\Anaconda3\Lib\site-packages\ipykernel\ipkernel.py", line 360, in do_execute
| res = shell.run_cell(code, store_history=store_history, silent=silent)
| File "C:\Users\T\Anaconda3\Lib\site-packages\ipykernel\zmqshell.py", line 532, in run_cell
| return super().run_cell(*args, **kwargs)
| File "C:\Users\T\Anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 2863, in run_cell
| result = self._run_cell(
| File "C:\Users\T\Anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 2909, in _run_cell
| return runner(coro)
| File "C:\Users\T\Anaconda3\Lib\site-packages\IPython\core\async_helpers.py", line 129, in _pseudo_sync_runner
| coro.send(None)
| File "C:\Users\T\Anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3106, in run_cell_async
| has_raised = await self.run_ast_nodes(code_ast.body, cell_name,
| File "C:\Users\T\Anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3309, in run_ast_nodes
| if await self.run_code(code, result, async_=asy):
| File "C:\Users\T\Anaconda3\Lib\site-packages\IPython\core\interactiveshell.py", line 3369, in run_code
| exec(code_obj, self.user_global_ns, self.user_ns)
| File "C:\Users\T\AppData\Local\Temp\ipykernel_23572\2140586109.py", line 1, in <cell line: 1>
| runfile('C:/Users/T/Downloads/pointPloter.py', wdir='C:/Users/T/Downloads')
| File "C:\Users\T\Anaconda3\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 577, in runfile
| exec_code(file_code, filename, ns_globals, ns_locals,
| File "C:\Users\T\Anaconda3\Lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 465, in exec_code
| exec(compiled, ns_globals, ns_locals)
| File "C:\Users\T\Downloads\pointPloter.py", line 109, in <module>
| v.animation()
| File "C:\Users\T\Downloads\pointPloter.py", line 103, in animation
| self.start()
| File "C:\Users\T\Downloads\pointPloter.py", line 83, in start
| QtGui.QApplication.instance().exec_()
| File "C:\Users\T\Anaconda3\Lib\site-packages\pyqtgraph\opengl\GLViewWidget.py", line 193, in paintGL
| self.drawItemTree(useItemNames=useItemNames)
| File "C:\Users\T\Anaconda3\Lib\site-packages\pyqtgraph\opengl\GLViewWidget.py", line 233, in drawItemTree
| self.drawItemTree(i, useItemNames=useItemNames)
| File "C:\Users\T\Anaconda3\Lib\site-packages\pyqtgraph\opengl\GLViewWidget.py", line 214, in drawItemTree
| debug.printExc()
| --- exception caught here ---
| File "C:\Users\T\Anaconda3\Lib\site-packages\pyqtgraph\opengl\GLViewWidget.py", line 211, in drawItemTree
| i.paint()
| File "C:\Users\T\Anaconda3\Lib\site-packages\pyqtgraph\opengl\items\GLScatterPlotItem.py", line 152, in paint
| glDrawArrays(GL_POINTS, 0, int(pos.size / pos.shape[-1]))
| AttributeError: 'list' object has no attribute 'size'
|==============================<<

在 0x0000026EB461B4C0 处绘制项目

我看到它给我一个属性错误。每次使用 size 属性时,我都测试了代码。但是,我无法使代码正常工作。我的代码是:

# -*- 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 obj in self.listPoints:
#print(obj.y)

for idObj, obj in enumerate(self.listPoints):
pts = [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):

pts = [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()

最佳答案

您应该将 np.array 传递给 GLScatterPlotItem,如下面的代码片段:

self.traces[idObj] = gl.GLScatterPlotItem(
pos=np.array(pts), # <- Here
color=pg.glColor((0, 6.5)),
size=3,
pxMode=True
)

关于python - 绘制项目时出错 <pyqtgraph.opengl.items.GLScatterPlotItem.GLScatterPlotItem object at 0x0000026EB461B4C0>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73515969/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com