How to set the same LUT for pg.HistogramLUTItem and pg.ImageItem?

Viewed 904

I want to use a particular colormap (LUT) to display a 2D array using pg.ImageItem and add a pg.HistogramLUTItem near it for interactive analysis. If I initialize the image passing it my LUT and then plot it in a window, everything is okay. However, when I create the pg.HistogramLUTItem and initialize it with my image, the colormap of the image instantly changes to that of the histogram. How should I set the proper colormap in the histogram?

Here is my minimal example:

import numpy as np
import pyqtgraph as pg

myLUT = np.array([[1.        , 1.        , 1.        ],
                  [0.38401946, 0.48864573, 0.963664  ],
                  [0.28766167, 0.81375253, 0.49518645],
                  [0.71970558, 0.92549998, 0.34362429]]) * 255

win = pg.GraphicsLayoutWidget()
win.show()
p1 = win.addPlot()
img = pg.ImageItem(np.random.rand(100, 100), lut=myLUT)
p1.addItem(img)
hist = pg.HistogramLUTItem()
hist.setImageItem(img)

... and here the image already changes to the "default" lut of the histogram.

I would expect the histogram to "inherit" the LUT of the image and automatically adopt the LookUpTable in the colorbar, but it is actually the other way around.

How can correct this behavior? Do I have to initialize the histogram with myLUT? I cannot see how to do that.

2 Answers

How about you start from a certain pyqtgraph colormap and then generate the LUT from it:

myLUT = colormap.getLookupTable()

And then you set the colormap ....

hist.gradient.setColorMap(colormap)

I don't think it can be done (but if someone can prove me wrong, please let me know...)

I think the problem is that the HistogramLUTItem uses a GradientEditorItem to create a lookup table. This class makes a LUT by linear interpolation of a few set points (the colored triangles next to the color bar). The GradientEditorItem cannot handle arbitrary LUTs and therefore the HistogramLUTItem class can't either.

The source code of HistogramLUTItem seems to confirm this. E.g. in the setImageItem method it sets the imageItem's LUT to that of the gradient editor, as you already suspected. Line 147 calls img.setLookupTable(self.getLookupTable), which in turn gets the GradientEditorItem's lookup table, self.lut = self.gradient.getLookupTable(n, alpha=alpha) on line 178.

I have made my own color bar class that can handle arbitrary lookup tables: PgColorBar. Feel free to use it.

Or you can create a PyQtGraph issue here.

Related