Skip to main content
`Ctrl`+`K`

# `ColormapDialog`

A QDialog widget to set-up the colormap.

It uses a description of colormaps as dict compatible with `Plot`.

To run the following sample code, a QApplication must be initialized.

Create the colormap dialog and set the colormap description and data range:

    >>> from silx.gui.dialog.ColormapDialog import ColormapDialog
    >>> from silx.gui.colors import Colormap

    >>> dialog = ColormapDialog()
    >>> colormap = Colormap(name='red', normalization='log',
    ...                     vmin=1., vmax=2.)

    >>> dialog.setColormap(colormap)
    >>> colormap.setVRange(1., 100.)  # This scale the width of the plot area
    >>> dialog.show()

Get the colormap description (compatible with `Plot`) from the dialog:

    >>> cmap = dialog.getColormap()
    >>> cmap.getName()
    'red'

It is also possible to display an histogram of the image in the dialog. This updates the data range with the range of the bins.

    >>> import numpy
    >>> image = numpy.random.normal(size=512 * 512).reshape(512, -1)
    >>> hist, bin_edges = numpy.histogram(image, bins=10)
    >>> dialog.setHistogram(hist, bin_edges)

The updates of the colormap description are also available through the signal: `ColormapDialog.sigColormapChanged`.

class ColormapDialog(parent=None, title='Colormap Dialog')

  A QDialog widget to set the colormap.

  Parameters:

    - parent – See `QDialog`

    - title (str) – The QDialog title

  visibleChanged

    This event is sent when the dialog visibility change

  showEvent(self, a0: QShowEvent | None)
  closeEvent(self, a0: QCloseEvent | None)
  hideEvent(self, a0: QHideEvent | None)
  close(self) → bool
  setModal(self, modal: bool)
  event(self, a0: QEvent | None) → bool
  exec(self) → int
  exec_(self) → int
  static computeDataRange(data)

    Compute the data range as used by `setDataRange()`.

    Parameters:

      data – The data to process

    Return type:

      List[Union[None,float]]

  static computeHistogram(data, scale='linear', dataRange=None)

    Compute the data histogram as used by `setHistogram()`.

    Parameters:

      - data – The data to process

      - dataRange – Optional range to compute the histogram, which is a tuple of min, max

    Return type:

      Tuple(List(float),List(float)

  setItem(item)

    Store the plot item.

    According to the state of the dialog, the item will be used to display the data range or the histogram of the data using `setDataRange()` and `setHistogram()`

  setData(data)

    Store the data

    According to the state of the dialog, the data will be used to display the data range or the histogram of the data using `setDataRange()` and `setHistogram()`

  setHistogram(hist=None, bin_edges=None)

    Set the histogram to display.

    This update the data range with the bounds of the bins.

    Parameters:

      - hist – array-like of counts or None to hide histogram

      - bin_edges – array-like of bins edges or None to hide histogram

  getColormap()

    Return the colormap description.

    Return type:

      Colormap

  resetColormap()

    Reset the colormap state before modification.

    ..note :: the colormap reference state is the state when set or the

      state when validated

  setDataRange(minimum=None, positiveMin=None, maximum=None)

    Set the range of data to use for the range of the histogram area.

    Parameters:

      - minimum (float) – The minimum of the data

      - positiveMin (float) – The positive minimum of the data

      - maximum (float) – The maximum of the data

  setColormapRangeFromDataBounds(bounds)

    Set the range of the colormap from current item and rect.

    If there is no ColormapMixIn item attached to the ColormapDialog, nothing is done.

    Parameters:

      bounds (Union[List[float],None]) – (xmin, xmax, ymin, ymax) Rectangular region in data space

  accept(self)
  storeCurrentState()

    save the current value sof the colormap if the user want to undo is modifications

  reject(self)
  setColormap(colormap)

    Set the colormap description

    Parameters:

      colormap (Colormap) – the colormap to edit

  keyPressEvent(event)

    Override key handling.

    It disables leaving the dialog when editing a text field.

    But several press of Return key can be use to validate and close the dialog.