pyclickimage.qt_profile_plot#

class pyclickimage.qt_profile_plot.QtProfilePlot(x: Sequence[float] | None = None, y: Sequence[float] | None = None, parent: QWidget | None = None)[source]#

Bases: QWidget

Interactive 1D line-profile plot widget.

QtProfilePlot is a lightweight Qt widget for displaying a sampled one-dimensional profile using only Qt painting primitives.

Two display orientations are supported. The orientation describes where the profile is intended to be placed relative to an image.

"horizontal"

Profile associated with the horizontal image direction.

The data coordinates are mapped as follows:

  • X is mapped to the horizontal screen direction.

  • Y is mapped to the vertical screen direction.

  • X ticks and labels are displayed at the bottom.

  • Y ticks and labels are displayed on the left.

This mode is therefore suitable for a profile displayed below an image.

The resulting coordinate system is the conventional Cartesian representation:

Y
^
|
|       /
|     /
|   /
+-----------------> X
"vertical"

Profile associated with the vertical image direction.

The data coordinates are mapped as follows:

  • X is mapped to the vertical screen direction, increasing from top to bottom (image convention).

  • Y is mapped to the horizontal screen direction.

  • X ticks and labels are displayed on the right.

  • Y ticks and labels are displayed at the bottom.

This mode is suitable for a profile displayed to the right of an image. All text remains horizontal and readable.

The widget does not physically rotate itself using QPainter.rotate(). Instead, the data-to-screen coordinate mapping is changed.

This approach has several advantages:

  • the QWidget keeps its normal Qt geometry;

  • the widget can be inserted normally into Qt layouts;

  • text remains horizontal and readable;

  • tick labels do not need to be rotated;

  • the same widget can be used below or beside an image;

  • mouse interaction can later use the same coordinate mapping.

Parameters:
  • x (Sequence[float], optional) – X coordinates of the sampled profile.

  • y (Sequence[float], optional) – Y values corresponding to x.

  • parent (QWidget, optional) – Parent Qt widget.

Raises:
  • ValueError – If only one of x or y is provided.

  • ValueError – If x and y have different lengths.

  • ValueError – If an invalid orientation is specified.

Examples

Create a standard horizontal profile:

plot = QtProfilePlot(
    x=[0, 1, 2, 3],
    y=[0, 2, 1, 4],
)

plot.orientation = "horizontal"

In this mode the profile is intended to be placed below an image.

Create a vertical profile:

plot.orientation = "vertical"

In this mode X is vertical and Y is horizontal, so the profile can be placed to the right of an image.

Configure axis titles:

plot.x_title = "Position"
plot.y_title = "Intensity"

Fix the Y axis with explicit ticks (the axis spans [min(ticks), max(ticks)]):

plot.y_ticks = [0, 51, 102, 153, 204, 255]

Go back to automatic ticks computed from the data:

plot.y_ticks = None

Sample a function:

plot.set_function(
    lambda x: x * x,
    0.0,
    10.0,
    samples=200,
)

Notes

The widget intentionally performs its own drawing rather than using Qt Charts or another plotting library.

The current implementation uses linear mappings for both axes.

Ticks and axis range. For each axis:

  • explicit ticks (a sequence of at least two distinct values) are used as given, and the axis spans [min(ticks), max(ticks)]; data outside this range are clipped to the plot area;

  • otherwise the axis spans the finite data range and grid + 1 equally spaced ticks are built.

The major grid lines are drawn at the tick positions.

See also

set_data

Set sampled profile data.

set_function

Sample and display a mathematical function.

set_orientation

Change the profile orientation.

HORIZONTAL = 'horizontal'#
VERTICAL = 'vertical'#
_axes(painter: QPainter, left: int, top: int, right: int, bottom: int, x_min: float, x_max: float, y_min: float, y_max: float, x_ticks: Sequence[float], y_ticks: Sequence[float]) → None[source]#

Draw axes, ticks, labels and titles.

horizontal mode

X axis is at the bottom. Y axis is at the left.

vertical mode

X axis is at the right. Y axis is at the bottom.

_color_property()[source]#

Create a QColor property.

static _draw_horizontal_grid(painter: QPainter, left: int, right: int, top: int, bottom: int, line_count: int, color: QColor, width: int) → None[source]#

Draw equally spaced horizontal grid lines.

Parameters:
  • painter (QPainter) – Painter used for rendering.

  • left (int) – Plot boundaries.

  • right (int) – Plot boundaries.

  • top (int) – Plot boundaries.

  • bottom (int) – Plot boundaries.

  • line_count (int) – Number of intervals.

  • color (QColor) – Grid line color.

  • width (int) – Grid line width.

Raises:

ValueError – If line_count <= 0.

static _draw_vertical_grid(painter: QPainter, left: int, right: int, top: int, bottom: int, line_count: int, color: QColor, width: int) → None[source]#

Draw equally spaced vertical grid lines.

Parameters:
  • painter (QPainter) – Painter used for rendering.

  • left (int) – Plot boundaries.

  • right (int) – Plot boundaries.

  • top (int) – Plot boundaries.

  • bottom (int) – Plot boundaries.

  • line_count (int) – Number of intervals.

  • color (QColor) – Grid line color.

  • width (int) – Grid line width.

Raises:

ValueError – If line_count <= 0.

_frame(painter: QPainter, left: int, top: int, right: int, bottom: int) → None[source]#

Draw the border around the plotting area.

Parameters:
  • painter (QPainter) – Painter used for rendering.

  • left (int) – Plot boundaries.

  • top (int) – Plot boundaries.

  • right (int) – Plot boundaries.

  • bottom (int) – Plot boundaries.

_grid(painter: QPainter, left: int, top: int, right: int, bottom: int, x_min: float, x_max: float, y_min: float, y_max: float, x_ticks: Sequence[float], y_ticks: Sequence[float]) → None[source]#

Draw the minor and major grid.

The minor grid is made of equally spaced lines. The major grid lines are drawn at the tick positions, so they always match the ticks and labels.

_labels_x(painter: QPainter, left: int, top: int, right: int, bottom: int, x_min: float, x_max: float, ticks: Sequence[float]) → None[source]#

Draw labels associated with the X data axis.

In horizontal mode, labels are displayed below the plot.

In vertical mode, labels are displayed to the right of the plot, x_min at the top and x_max at the bottom.

_labels_y(painter: QPainter, left: int, top: int, right: int, bottom: int, y_min: float, y_max: float, ticks: Sequence[float]) → None[source]#

Draw labels associated with the Y data axis.

In horizontal mode, labels are displayed to the left.

In vertical mode, labels are displayed below the plot.

_line(painter: QPainter, left: int, top: int, right: int, bottom: int, x_min: float, x_max: float, y_min: float, y_max: float) → None[source]#

Draw the sampled profile.

Parameters:
  • painter (QPainter) – Painter used for rendering.

  • left (int) – Plot boundaries.

  • top (int) – Plot boundaries.

  • right (int) – Plot boundaries.

  • bottom (int) – Plot boundaries.

  • x_min (float) – X axis range.

  • x_max (float) – X axis range.

  • y_min (float) – Y axis range.

  • y_max (float) – Y axis range.

Notes

The line is clipped to the plot area, so values outside an explicit tick range do not overflow on the labels. Non-finite samples break the line.

_map_x(value: float, left: int, right: int, top: int, bottom: int, x_min: float, x_max: float) → float[source]#

Map an X data value to its screen coordinate.

Parameters:
  • value (float) – X data value.

  • left (int) – Plot boundaries.

  • right (int) – Plot boundaries.

  • top (int) – Plot boundaries.

  • bottom (int) – Plot boundaries.

  • x_min (float) – X data range.

  • x_max (float) – X data range.

Returns:

Horizontal screen coordinate in horizontal mode, vertical screen coordinate in vertical mode.

Return type:

float

Notes

vertical mode follows the image convention: x_min at the top, x_max at the bottom.

_map_y(value: float, left: int, right: int, top: int, bottom: int, y_min: float, y_max: float) → float[source]#

Map a Y data value to its screen coordinate.

Parameters:
  • value (float) – Y data value.

  • left (int) – Plot boundaries.

  • right (int) – Plot boundaries.

  • top (int) – Plot boundaries.

  • bottom (int) – Plot boundaries.

  • y_min (float) – Y data range.

  • y_max (float) – Y data range.

Returns:

Vertical screen coordinate in horizontal mode, horizontal screen coordinate in vertical mode.

Return type:

float

_paint_plot(painter: QPainter) → None[source]#

Paint the complete plotting area.

static _parse_ticks(value: Sequence[float] | None) → list[float] | None[source]#

Convert a tick sequence to a list of floats.

Parameters:

value (Sequence[float] or None) – Tick positions.

Returns:

None if value is None, otherwise the values as floats.

Return type:

list of float or None

Raises:

ValueError – If a tick is not finite.

static _resolve_axis(ticks: list[float] | None, values: Sequence[float], intervals: int) → tuple[list[float], float, float][source]#

Return the ticks and the range of one axis.

Parameters:
  • ticks (list of float or None) – Explicit ticks.

  • values (Sequence[float]) – Data values along the axis.

  • intervals (int) – Number of intervals of the automatic ticks.

Returns:

  • ticks (list of float) – Sorted tick positions.

  • vmin, vmax (float) – Axis range, with vmin < vmax.

Raises:

ValueError – If automatic ticks are needed and intervals <= 0.

Notes

  • Explicit ticks with at least two distinct values: used as given, the range is [min(ticks), max(ticks)].

  • Otherwise: the range is the finite data range (widened by 0.5 on each side when constant, [0, 1] without finite value) and intervals + 1 equally spaced ticks are built.

_ticks_x(painter: QPainter, left: int, top: int, right: int, bottom: int, x_min: float, x_max: float, ticks: Sequence[float]) → None[source]#

Draw ticks associated with the X data axis.

In horizontal mode, X ticks are displayed at the bottom.

In vertical mode, X ticks are displayed on the right.

_ticks_y(painter: QPainter, left: int, top: int, right: int, bottom: int, y_min: float, y_max: float, ticks: Sequence[float]) → None[source]#

Draw ticks associated with the Y data axis.

In horizontal mode, Y ticks are displayed on the left.

In vertical mode, Y ticks are displayed at the bottom.

_titles(painter: QPainter, left: int, top: int, right: int, bottom: int) → None[source]#

Draw X and Y axis titles.

horizontal mode
  • X title: centered below the plot.

  • Y title: horizontal, centered above the Y axis.

vertical mode
  • X title: vertical, rotated 90 degrees, to the right of the plot.

  • Y title: horizontal, centered below the plot.

property background_color#
clear() → None[source]#

Remove all plotted data.

The widget itself remains visible, but the profile is removed.

property line_color#
property line_width: int#

Width of the plotted line in pixels.

Type:

int

property orientation: str#

Current plot orientation.

Returns:

Either "horizontal" or "vertical".

Return type:

str

Notes

"horizontal" means:

  • X is horizontal.

  • Y is vertical.

  • X axis is at the bottom.

  • Y axis is at the left.

"vertical" means:

  • X is vertical (increasing from top to bottom).

  • Y is horizontal.

  • X axis is at the right.

  • Y axis is at the bottom.

Type:

str

paintEvent(event) → None[source]#

Paint the complete widget.

Parameters:

event (QPaintEvent) – Qt paint event.

set_data(x: Sequence[float], y: Sequence[float]) → None[source]#

Set the sampled profile data.

Parameters:
  • x (Sequence[float]) – X coordinates.

  • y (Sequence[float]) – Y values.

Raises:

ValueError – If x and y contain different numbers of values.

Notes

Values are converted to float before being stored. Non-finite values (NaN, inf) are allowed: they are ignored when computing the automatic range and break the line.

set_function(function: Callable[[float], float], x_min: float, x_max: float, samples: int = 500) → None[source]#

Sample a function and display it.

Parameters:
  • function (Callable[[float], float]) – Function to evaluate.

  • x_min (float) – Minimum X value.

  • x_max (float) – Maximum X value.

  • samples (int, default=500) – Number of uniformly spaced samples.

Raises:
  • ValueError – If samples < 2.

  • ValueError – If x_max <= x_min.

set_orientation(value: str) → None[source]#

Set the logical orientation of the profile.

Parameters:

value (str) – Either "horizontal" or "vertical".

Raises:

ValueError – If value is not a supported orientation.

Notes

The QWidget itself is never rotated.

In horizontal mode:

screen_x = X
screen_y = Y

In vertical mode:

screen_x = Y
screen_y = X

This makes the vertical mode suitable for a profile displayed beside the right side of an image.

property show_axes: bool#

Whether the main axes are displayed.

Type:

bool

property show_grid: bool#

Whether major grid lines are displayed.

Type:

bool

property show_labels: bool#

Whether tick labels are displayed.

Type:

bool

property show_minor_grid: bool#

Whether minor grid lines are displayed.

Type:

bool

property show_ticks: bool#

Whether tick marks are displayed.

Type:

bool

property x: list[float]#

Copy of the X coordinates.

Type:

list[float]

property x_formatter: Callable[[float], str]#

Formatter for X tick labels.

Type:

Callable[[float], str]

property x_ticks: list[float] | None#

Explicit X-axis tick positions.

A sequence of at least two distinct values is used as given: the X axis then spans [min(x_ticks), max(x_ticks)]. Otherwise (None, fewer than two values, or all values equal), equally spaced ticks are built from the data range.

Type:

list[float] or None

property x_title: str#

Title associated with the X data axis.

Type:

str

property y: list[float]#

Copy of the Y values.

Type:

list[float]

property y_formatter: Callable[[float], str]#

Formatter for Y tick labels.

Type:

Callable[[float], str]

property y_ticks: list[float] | None#

Explicit Y-axis tick positions.

A sequence of at least two distinct values is used as given: the Y axis then spans [min(y_ticks), max(y_ticks)], and values outside are clipped to the plot area. Otherwise (None, fewer than two values, or all values equal), equally spaced ticks are built from the data range.

Type:

list[float] or None

property y_title: str#

Title associated with the Y data axis.

Type:

str