I am trying to set a color gradient for my bar plot in pyqtgraph -- the goal is to make a gradient that changes color in the y-direction.
So far, I have this code snippet to attempt to make a gradient, but the bars are not painted (or brushed):
from pyqtgraph.Qt import QtGui, QtCore
import pyqtgraph as pg
# Make gradient for bar plot
grad = QtGui.QLinearGradient(0, 0, 0, 3)
grad.setColorAt(0.1, pg.mkColor('#000000'))
grad.setColorAt(0.9, pg.mkColor('b'))
brush = QtGui.QBrush(grad)
# Attempt to add gradient to bar plot
self.bar = pg.BarGraphItem(x=data_x, height=data_y, width=700, brush=brush)
self.win = pg.plot()
self.win.addItem(self.bar, ignoreBounds=False)
QGradients have different coordinate modes.
The default mode is LogicalMode
, meaning that the coordinates set for that gradient and used for painting use logical vaues (as in "pixels").
Since pyqtgraph items often show even small data values and use relative coordinates, this results in your gradient not fully visible, probably because the values of those bars are too small.
Limit the coordinates to the 0-1 range, and then set a relative coordinate mode:
grad = QtGui.QLinearGradient(0, 0, 0, 1)
grad.setCoordinateMode(QtGui.QGradient.ObjectBoundingMode)
Note that since Qt5.12 ObjectBoundingMode has been deprecated and ObjectMode
should be used instead.