我试图通过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()


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


  追溯(最近一次通话最后一次):文件“”,第1行,在
  
  文件
  “ C:\ Python34 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ ScatterPlotItem.py”,
  setBrush中的第467行
      self.updateSpots(dataSet)
  
  文件
  “ C:\ Python34 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ ScatterPlotItem.py”,
  第553行,在updateSpots中
      self.fragmentAtlas.getAtlas()#生成图集,因此源宽度可用。
  
  文件
  “ C:\ Python34 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ ScatterPlotItem.py”,
  getAtlas中的第193行
      self.buildAtlas()
  
  文件
  “ C:\ Python34 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ ScatterPlotItem.py”,
  在buildAtlas中的第143行
      img = renderSymbol(key [0],key [1],sourceRect.pen,sourceRect.brush)
  
  文件
  “ C:\ Python34 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ ScatterPlotItem.py”,
  第74行,在renderSymbol中
      drawSymbol(p,符号,大小,笔,画笔)
  
  文件
  “ C:\ Python34 \ lib \ site-packages \ pyqtgraph \ graphicsItems \ ScatterPlotItem.py”,
  第49行,在drawSymbol中
      painter.setBrush(brush)TypeError:参数与任何重载调用都不匹配:QPainter.setBrush(QBrush):参数1具有
  意外的类型'str'QPainter.setBrush(Qt.BrushStyle):参数1
  具有意外的类型'str'


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

最佳答案

添加s.setBrush(QtGui.QBrush({your parameters}))
例如:


QtGui.QBrush()
QtGui.QBrush(Qt.BrushStyle bs)
QtGui.QBrush(QColor颜色,Qt.BrushStyle样式= Qt.SolidPattern)
QtGui.QBrush(Qt.GlobalColor颜色,Qt.BrushStyle样式= Qt.SolidPattern)
QtGui.QBrush(QColor颜色,QPixmap像素图)
QtGui.QBrush(Qt.GlobalColor颜色,QPixmap像素图)
QtGui.QBrush(QPixmap像素图)
QtGui.QBrush(QImage图片)
QtGui.QBrush(QGradient梯度)
QtGui.QBrush(QBrush画笔)
QtGui.QBrush(QVariant变体)


在您的代码中:

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

app = QtGui.QApplication(sys.argv)
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]

# Create seed for the random
time = QtCore.QTime.currentTime()
QtCore.qsrand(time.msec())

for i in range(len(x)):
    s = pg.ScatterPlotItem([x[i]], [y[i]], size=10, pen=pg.mkPen(None))  # brush=pg.mkBrush(255, 255, 255, 120))
    s.setBrush(QtGui.QBrush(QtGui.QColor(QtCore.qrand() % 256, QtCore.qrand() % 256, QtCore.qrand() % 256)))
    w1.addItem(s)
mw.show()
sys.exit(QtGui.QApplication.exec_())


输出:

python - pyqtgraph散点图刷-LMLPHP

如果要为前5个点(x:1-5)设置红色,为另外5个点(x:6-10)设置蓝色

import sys
from pyqtgraph.Qt import QtGui
import pyqtgraph as pg

app = QtGui.QApplication(sys.argv)
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]

color = QtGui.QColor("#0000FF")
s = pg.ScatterPlotItem(x[:5], y[:5], size=10, pen=pg.mkPen(None), brush='r')
w1.addItem(s)
s = pg.ScatterPlotItem(x[5:], y[5:], size=10, pen=pg.mkPen(None), brush='b')
w1.addItem(s)
mw.show()
sys.exit(QtGui.QApplication.exec_())


python - pyqtgraph散点图刷-LMLPHP

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

10-09 20:05