Skip to content

Colormap utilities#

See also:

superqt.cmap.draw_colormap(painter_or_device, cmap, rect=None, border_color=None, border_width=1, lighter=100, checkerboard_size=4) #

Draw a colormap onto a QPainter or QPaintDevice.

Parameters:

Name Type Description Default
painter_or_device QPainter | QPaintDevice

A QPainter instance or a QPaintDevice (e.g. a QWidget or QPixmap) onto which to paint the colormap.

required
cmap Colormap | Any

cmap.Colormap instance, or anything that can be converted to one (such as a string name of a colormap in the cmap catalog). https://cmap-docs.readthedocs.io/en/latest/colormaps/#colormaplike-objects

required
rect QRect | QRectF | None

A rect onto which to draw. If None, the painter.viewport() will be used. by default None

None
border_color QColor | str | None

If not None, a border of color border_color and width border_width is included around the edge, by default None.

None
border_width int

The width of the border to draw (provided border_color is not None), by default 2

1
lighter int

Percentage by which to lighten (or darken) the colors. Greater than 100 lightens, less than 100 darkens, by default 100 (i.e. no change).

100
checkerboard_size bool

Size (in pixels) of the checkerboard pattern to draw, by default 5. If 0, no checkerboard is drawn.

4

Examples:

from qtpy.QtGui import QPixmap
from qtpy.QtWidgets import QWidget
from superqt.utils import draw_colormap

viridis = 'viridis'  # or cmap.Colormap('viridis')

class W(QWidget):
    def paintEvent(self, event) -> None:
        draw_colormap(self, viridis, event.rect())

# or draw onto a QPixmap
pm = QPixmap(200, 200)
draw_colormap(pm, viridis)

superqt.cmap.QColormapLineEdit #

Bases: QLineEdit

A QLineEdit that shows a colormap swatch.

When the current text is a valid colormap name from the cmap package, a swatch of the colormap will be shown to the left of the text (if fractionalColormapWidth is less than .75) or behind the text (for when the colormap fills the full width).

If the current text is not a valid colormap name, a swatch of the fallback colormap will be shown instead (by default, a gray colormap) if fractionalColormapWidth is less than .75.

Parameters:

Name Type Description Default
parent QWidget

The parent widget.

None
fractional_colormap_width float

The fraction of the widget width to use for the colormap swatch. If the colormap is full width (greater than 0.75), the swatch will be drawn behind the text. Otherwise, the swatch will be drawn to the left of the text. Default is 0.33.

0.33
fallback_cmap Colormap | str | None

The colormap to use when the current text is not a recognized colormap. by default "gray".

'gray'
missing_icon QIcon | StandardPixmap

The icon to show when the current text is not a recognized colormap and fractionalColormapWidth is less than .75. Default is a question mark.

MISSING
checkerboard_size int

Size (in pixels) of the checkerboard pattern to draw behind colormaps with transparency, by default 4. If 0, no checkerboard is drawn.

4

superqt.cmap.QColormapItemDelegate #

Bases: QStyledItemDelegate

Delegate that draws colormaps into a QAbstractItemView item.

Parameters:

Name Type Description Default
parent QObject

The parent object.

None
item_size QSize

The size hint for each item, by default QSize(80, 22).

DEFAULT_SIZE
fractional_colormap_width float

The fraction of the widget width to use for the colormap swatch. If the colormap is full width (greater than 0.75), the swatch will be drawn behind the text. Otherwise, the swatch will be drawn to the left of the text. Default is 0.33.

1
padding int

The padding (in pixels) around the edge of the item, by default 1.

1
checkerboard_size int

Size (in pixels) of the checkerboard pattern to draw behind colormaps with transparency, by default 4. If 0, no checkerboard is drawn.

4