use QGraphicsPixmapItem instead of ImageItem to draw colorbar#2781
use QGraphicsPixmapItem instead of ImageItem to draw colorbar#2781j9ac9k merged 1 commit intopyqtgraph:masterfrom
Conversation
|
Nice Find! |
|
Hi @pijyoi , I'll admit to laziness here, but there was a bit of a reason why that was handled by an image:
I think these points might be worth a quick consideration; and they can probably be addressed by tweaking the gradient settings a bit, rather than keeping the image. Offsetting the start and end point by half a unit, maybe? Maybe Qt does that anyway? I don't think stepped/categorical color maps commonly use a color bar... But if there's a "nearest neighbor" setting for the gradient, that might be interesting to expose. Thank you for cleaning up the code :) |
|
The following script demonstrates what @NilsNemitz refers to in his two points, i.e. blurring and the half "slot" syndrome at the edges. From what I understand, the first point boils down to whether a colormap is being treated as a gradient or as a look-up-table. import itertools
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui, QtWidgets
pg.mkQApp()
pw = pg.PlotWidget()
pw.show()
pi = pw.getPlotItem()
cycle = itertools.cycle([(255,0,0),(0,255,0),(0,0,255)])
colors = [next(cycle) for x in range(256)]
cmap = pg.ColorMap(None, colors)
data = np.ones(10)[:,np.newaxis] * np.arange(256)
img = pg.ImageItem(image=data, axisOrder='row-major')
pi.addItem(img)
pi.addColorBar(img, colorMap=cmap, orientation='horizontal', interactive=False)
rect = QtWidgets.QGraphicsRectItem(0,11,256,10)
rect.setPen(QtGui.QPen(QtCore.Qt.PenStyle.NoPen))
grad = cmap.getGradient()
grad.setCoordinateMode(QtGui.QGradient.CoordinateMode.ObjectMode)
rect.setBrush(grad)
pi.addItem(rect)
pg.exec() |
|
So if using a gradient does not accurately represent a discrete lookup-table, a lightweight replacement would be a greatly simplified version of |
|
I like that as a solution, if it works out, it will be a very neat! But --if-- it turns out to be unpleasantly complex, let me say that I wouldn't be violently opposed to the gradient solution either:
|
f2917d3 to
ce8b09c
Compare
|
Nice find on the PixmapItem @pijyoi Thanks for the PR, LGTM, merging. |
Rather than using a heavy-weight
ImageItem, a colorbar can be rendered by drawing a rectangle using a gradient brush.(both
GradientLegendandGLGradientLegendItemalready do that)