-
-
Save aeroaks/16542cb110ef31520fd9f05a3607fcde to your computer and use it in GitHub Desktop.
PyQtGraph colorbar class (using hsv color)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pyqtgraph as pg | |
class ColorBar(pg.GraphicsObject): | |
def __init__(self, cmap, width, height, ticks=None, tick_labels=None, label=None): | |
pg.GraphicsObject.__init__(self) | |
# handle args | |
label = label or '' | |
w, h = width, height | |
stops, colors = cmap.getStops('float') | |
smn, spp = stops.min(), stops.ptp() | |
stops = (stops - stops.min())/stops.ptp() | |
if ticks is None: | |
ticks = np.r_[0.0:1.0:5j, 1.0] * spp + smn | |
tick_labels = tick_labels or ["%0.2g" % (t,) for t in ticks] | |
# setup picture | |
self.pic = pg.QtGui.QPicture() | |
p = pg.QtGui.QPainter(self.pic) | |
# draw bar with gradient following colormap | |
p.setPen(pg.mkPen('k')) | |
grad = pg.QtGui.QLinearGradient(w/2.0, 0.0, w/2.0, h*1.0) | |
for stop, color in zip(stops, colors): | |
grad.setColorAt(1.0 - stop, pg.QtGui.QColor(*[255*c for c in color])) | |
p.setBrush(pg.QtGui.QBrush(grad)) | |
p.drawRect(pg.QtCore.QRectF(0, 0, w, h)) | |
# draw ticks & tick labels | |
mintx = 0.0 | |
for tick, tick_label in zip(ticks, tick_labels): | |
y_ = (1.0 - (tick - smn)/spp) * h | |
p.drawLine(0.0, y_, -5.0, y_) | |
br = p.boundingRect(0, 0, 0, 0, pg.QtCore.Qt.AlignRight, tick_label) | |
if br.x() < mintx: | |
mintx = br.x() | |
p.drawText(br.x() - 10.0, y_ + br.height() / 4.0, tick_label) | |
# draw label | |
br = p.boundingRect(0, 0, 0, 0, pg.QtCore.Qt.AlignRight, label) | |
p.drawText(-br.width() / 2.0, h + br.height() + 5.0, label) | |
# done | |
p.end() | |
# compute rect bounds for underlying mask | |
self.zone = mintx - 12.0, -15.0, br.width() - mintx, h + br.height() + 30.0 | |
def paint(self, p, *args): | |
# paint underlying mask | |
p.setPen(pg.QtGui.QColor(255, 255, 255, 0)) | |
p.setBrush(pg.QtGui.QColor(255, 255, 255, 200)) | |
p.drawRoundedRect(*(self.zone + (9.0, 9.0))) | |
# paint colorbar | |
p.drawPicture(0, 0, self.pic) | |
def boundingRect(self): | |
return pg.QtCore.QRectF(self.pic.boundingRect()) | |
if __name__ == '__main__': | |
app = pg.mkQApp() | |
# to use HSV colors for mapping! | |
vals = np.arange(-1.0,1,0.1) | |
clr = np.zeros((vals.shape[0], 4)) | |
# normalising values between 0 and 1, even if it has -ve values | |
cent = (vals - vals.min(0)) / vals.ptp(0) | |
for i, x in enumerate(cent): | |
clr[i] = np.array(pg.hsvColor(x).getRgbF()) | |
# use less ink | |
pg.setConfigOption('foreground', 'k') | |
pw = pg.plot() | |
# make colormap | |
stops = np.r_[np.arange(vals.min(), vals.max(), (vals.max() - vals.min())/vals.shape[0])] | |
colors = clr | |
cm = pg.ColorMap(stops, colors) | |
# make colorbar, placing by hand | |
cb = ColorBar(cm, 10, 200, label='Foo (Hz)')#, [0., 0.5, 1.0]) | |
pw.scene().addItem(cb) | |
cb.translate(570.0, 90.0) | |
# add data colored with colormap | |
for i, x in enumerate(vals): | |
pw.plot([i], [i], pen=None, symbol='o', symbolBrush=cm.map(x, 'qcolor')) | |
app.exec_() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment