pythonimageuser-interfacepyqtpyqtgraph

How to set the default colour of a PyQtGraph ImageView's Histogram LUT?


PyQtGraph's ImageView widget includes a histogram with:

  1. a moveable region that defines dark and light levels. as well as
  2. the ability to edit a colour gradient.

These two features allow users to edit/generate a colour lookup table (LUT) to pseudo-colour the displayed image.

By default, the colour gradient is set to black and white as shown below:

Default PyQtGraph ImageView histogram LUT

How can one set a default gradient to some other than the default black and white?

Minimal Reproducible Example Code

import numpy as np
import pyqtgraph as pg

data = np.array(
    [
        [0.0, 0.0, 0.5, 2.5, 2.5, 3.5],
        [2.0, 2.0, 1.5, 0.5, 2.5, 3.5],
        [2.0, 2.0, 2.0, 2.5, 0.5, 1.5],
        [3.0, 3.0, 2.5, 3.0, 1.5, 0.5],
        [3.0, 3.0, 3.0, 4.5, 1.5, 1.5],
    ]
)

# Get QApplication instance if it exists, else create new one
app = pg.Qt.mkQApp("Example Cost Matrix Visualisation")

# Create and configure plot
plot = pg.PlotItem()
plot.setTitle("Cost Matrix")
plot.setLabel(axis="top", text="Y Signal")
plot.setLabel(axis="left", text="X Signal")

# Create image item and view
im = pg.ImageItem(data, axisOrder="row-major")
imv = pg.ImageView(imageItem=im, view=plot)
imv.show()
imv.setHistogramLabel("Cost Matrix Histogram")

app.exec()

Solution

  • The GradientEditorItem assoicated with a HistogramLUTItem of an ImageView is responsible for defining the gradient used to create the LUT for pseudo-colouring images. One can change the gradient of the GradientEditorItem by either specifiying their own gradient via RGB valued ticks at locations between 0.0 and 1.0 using restoreState:

    hist_lut_item = image_view.getHistogramWidget().item
    hist_lut_item.gradient.restoreState(
        {"mode": "rgb", "ticks": [(0.00, (0, 0, 255)), (1.00, (255, 0, 0))]}
    )
    

    This produces the following:

    Custom PyQtGraph ImageView histogram LUT

    Or by using a preset (a list of valid presets be found in GradientPresets.py):

    hist_lut_item = image_view.getHistogramWidget().item
    hist_lut_item.gradient.loadPreset('thermal')
    

    Minimal Reproducible Example Code

    import numpy as np
    import pyqtgraph as pg
    
    data = np.array(
        [
            [0.0, 0.0, 0.5, 2.5, 2.5, 3.5],
            [2.0, 2.0, 1.5, 0.5, 2.5, 3.5],
            [2.0, 2.0, 2.0, 2.5, 0.5, 1.5],
            [3.0, 3.0, 2.5, 3.0, 1.5, 0.5],
            [3.0, 3.0, 3.0, 4.5, 1.5, 1.5],
        ]
    )
    
    # Get QApplication instance if it exists, else create new one
    app = pg.Qt.mkQApp("Example Cost Matrix Visualisation")
    
    # Create and configure plot
    plot = pg.PlotItem()
    plot.setTitle("Cost Matrix")
    plot.setLabel(axis="top", text="Y Signal")
    plot.setLabel(axis="left", text="X Signal")
    
    # Create image item and view
    im = pg.ImageItem(data, axisOrder="row-major")
    imv = pg.ImageView(imageItem=im, view=plot)
    imv.show()
    imv.setHistogramLabel("Cost Matrix Histogram")
    
    # Get HistogramLUTItem
    ht = imv.getHistogramWidget().item
    
    # Specify own gradient via RGB valued ticks at locations between 0.0 and 1.0
    ht.gradient.restoreState(
        {"mode": "rgb", "ticks": [(0.00, (0, 0, 255)), (1.00, (255, 0, 0))]}
    )
    
    # # Alternatively, use one of the default presets. See all options here:
    # # https://github.com/pyqtgraph/pyqtgraph/blob/master/pyqtgraph/graphicsItems/GradientPresets.py
    # ht.gradient.loadPreset("thermal")
    
    app.exec()