gpt4 book ai didi

python - pyqtgraph 散点图项 setbrush

转载 作者:行者123 更新时间:2023-12-01 03:29:41 25 4
gpt4 key购买 nike

我正在尝试通过 setBrush 传递列表参数。

from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
import numpy as np

app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
mw.resize(800,800)
view = pg.GraphicsLayoutWidget()
mw.setCentralWidget(view)
mw.setWindowTitle('pyqtgraph example: ScatterPlot')
w1 = view.addPlot()
x = [1,2,3,4,5,6,7,8,9,10]
y = [10,8,6,4,2,20,18,16,14,12]
s1 = pg.ScatterPlotItem(x,y,size=10, pen=pg.mkPen(None), brush=pg.mkBrush(255, 255, 255, 120))
s1.setBrush(['r']*10, mask=None)
w1.addItem(s1)
mw.show()

当我运行此代码时,出现如下错误:

Traceback (most recent call last): File "", line 1, in

File "C:\Python34\lib\site-packages\pyqtgraph\graphicsItems\ScatterPlotItem.py", line 467, in setBrush self.updateSpots(dataSet)

File "C:\Python34\lib\site-packages\pyqtgraph\graphicsItems\ScatterPlotItem.py", line 553, in updateSpots self.fragmentAtlas.getAtlas() # generate atlas so source widths are available.

File "C:\Python34\lib\site-packages\pyqtgraph\graphicsItems\ScatterPlotItem.py", line 193, in getAtlas self.buildAtlas()

File "C:\Python34\lib\site-packages\pyqtgraph\graphicsItems\ScatterPlotItem.py", line 143, in buildAtlas img = renderSymbol(key[0], key[1], sourceRect.pen, sourceRect.brush)

File "C:\Python34\lib\site-packages\pyqtgraph\graphicsItems\ScatterPlotItem.py", line 74, in renderSymbol drawSymbol(p, symbol, size, pen, brush)

File "C:\Python34\lib\site-packages\pyqtgraph\graphicsItems\ScatterPlotItem.py", line 49, in drawSymbol painter.setBrush(brush) TypeError: arguments did not match any overloaded call: QPainter.setBrush(QBrush): argument 1 has unexpected type 'str' QPainter.setBrush(Qt.BrushStyle): argument 1 has unexpected type 'str'

如何通过 setBrush 传递列表?我必须为不同的点分配不同的颜色。

最佳答案

如果您想更改点的颜色,可以将列表传递给 symbolBrush 关键字。然后您的代码将变为:

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

app = QtGui.QApplication([])
mw = QtGui.QMainWindow()
mw.resize(800,800)
view = pg.GraphicsLayoutWidget()
mw.setCentralWidget(view)
mw.setWindowTitle('pyqtgraph example: ScatterPlot')
w1 = view.addPlot()
x = [1,2,3,4,5,6,7,8,9,10]
y = [10,8,6,4,2,20,18,16,14,12]

brush_list = [pg.mkColor(c) for c in "rgbcmykwrg"]

p1 = w1.plot()
s1 = p1.setData(x=x, y=y, size=10, pen=pg.mkPen(None), brush='g', symbol='o', symbolBrush=brush_list, symbolPen='r')
mw.show()
sys.exit(QtGui.QApplication.exec_())

我需要执行 p1 = w1.plot() 才能使其工作,但我对所有 pyqtgraph 对象的了解不够流畅,无法告诉您它的作用。

我必须将颜色字符串(或元组或其他)转换为 pg.mkColor() 对象才能工作(尽管文档说不需要)

请注意,brush 关键字会填充图形,直到达到您指定的某个值(例如 x 轴),这对于线很有用,但不会显示点的任何内容,您可以请参见图片。

symbolPen 关键字可用于点的线条颜色。

enter image description here

关于python - pyqtgraph 散点图项 setbrush,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41060163/

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