1
votes

I am trying to pass list argument through 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()

When I run this code, I am getting error as follows:

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'

How can I pass list though setBrush? I have to assign different color to different points.

2

2 Answers

1
votes

add s.setBrush(QtGui.QBrush({your parameters})) by example:

  • QtGui.QBrush()
  • QtGui.QBrush(Qt.BrushStyle bs)
  • QtGui.QBrush(QColor color, Qt.BrushStyle style = Qt.SolidPattern)
  • QtGui.QBrush(Qt.GlobalColor color, Qt.BrushStyle style = Qt.SolidPattern)
  • QtGui.QBrush(QColor color, QPixmap pixmap)
  • QtGui.QBrush(Qt.GlobalColor color, QPixmap pixmap)
  • QtGui.QBrush(QPixmap pixmap)
  • QtGui.QBrush(QImage image)
  • QtGui.QBrush(QGradient gradient)
  • QtGui.QBrush(QBrush brush)
  • QtGui.QBrush(QVariant variant)

In your Code:

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_())

Output:

enter image description here

If you want to set the red color for the first 5 points (x: 1-5) and blue for another 5 points (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_())

enter image description here

1
votes

If you want to change the color of your dots, you can pass a list to the symbolBrush keyword. Your code then becomes:

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_())

I needed to do p1 = w1.plot() to make it work, but I'm not fluent enough in all the pyqtgraph objects to tell you what that does.

I had to convert the color string (or tuple or whatever) into a pg.mkColor() object for it to work (although the docs say it isn't needed)

Note that the brush keyword fills the graph until a certain value you specify (e.g. the x-axis), which is useful for lines, but doesn't show anything for points, as you can see in the image.

The symbolPen keyword can be used for the line color of your points.

enter image description here