GradientWidget, example

139 views
Skip to first unread message

Leonid Rumatov

unread,
Jun 24, 2015, 7:56:04 AM6/24/15
to pyqt...@googlegroups.com
Hi everybody.

I use the ImageItem, and want to show gradient legend for it. 

Is there easy way to create it, like "addLegend" for plotItems?

Here is my palette:

grd_min, grd_max = grd.min(), grd.max()
trans = 255
palette = [(255, 0, 0, trans), (255, 92, 0, trans), (255, 255, 0, trans), (0, 255, 0, trans),
(0, 255, 255, trans), (0, 0, 255, trans), (255, 0, 255, trans)]
cm = pg.ColorMap(np.linspace(grd_min, grd_max, num=len(palette), endpoint=True), palette)
lut = cm.getLookupTable(grd_min, grd_max)
img.setImage(grd)
img.setRect(rect)
img.setLookupTable(lut)

where img is ImageItem. 

I make GradientLegend manually:

# Gradient legend:
gl = pg.GradientLegend((10, 200), (10, 30))
gradient = QtGui.QLinearGradient()
for pos, color in zip(*cm.getStops(1)):
gradient.setColorAt((pos-grd_min)/(grd_max - grd_min), QtGui.QColor(*color))
gl.setGradient(gradient)

pi.vb.addItem(gl)
#gl.scale(1, -1)
gl.setLabels({'asdhffdfdfh': 1625.0})

I think, this is ugly. And here is a problem: labels isn't shown.
I suspect, this is because of problem with coordinates. Both variants - with gl.scale and without it.

Is there any way to create GradientLegend without such large code? I think, this is typical task. 


Leonid Rumatov

unread,
Jun 24, 2015, 8:04:48 AM6/24/15
to pyqt...@googlegroups.com
I have solved the last problem, with labels. The values must be between (0; 1):

labels = {str(k):(k-grd_min)/(grd_max-grd_min) for k in iso_levels}

среда, 24 июня 2015 г., 16:56:04 UTC+5 пользователь Leonid Rumatov написал:
Reply all
Reply to author
Forward
0 new messages