pyclickimage.qt_histogram#
- class pyclickimage.qt_histogram.QtHistogram(data: Sequence[float] | None = None, bins: Sequence[float] | None = None, edges: Sequence[float] | None = None, parent: QWidget | None = None)[source]#
Bases:
QWidgetInteractive histogram and cumulative-distribution widget.
The widget displays binned numeric data using four display modes:
histogram with absolute counts;
histogram with percentages;
cumulative histogram with absolute counts;
cumulative histogram with percentages.
The underlying histogram always stores absolute bin values. Display transformations are applied only while painting, so changing the display mode never modifies the source data.
- Parameters:
data (Sequence[float], optional) – Raw numeric samples used to build the histogram. Values are converted to
float.bins (Sequence[float], optional) – Explicit histogram heights. Values must be non-negative. When provided,
edgesmust containlen(bins) + 1values.edges (Sequence[float], optional) – Monotonically increasing bin boundaries. When omitted with
bins, integer boundaries[0, 1, ..., len(bins)]are used.parent (QWidget, optional) – Parent Qt widget.
Notes
set_datacomputes equally spaced bins when no explicit edges are supplied.set_binsaccepts already-computed histogram values.Percentage mode divides each displayed value by the sum of the original bin values and multiplies it by 100. In cumulative percentage mode, the final value is therefore 100 when the histogram contains data.
The two display options are controlled by checkboxes placed below the histogram.
Examples
>>> histogram = QtHistogram(data=[1, 2, 2, 3, 3, 3]) >>> histogram.cumulative = True >>> histogram.percentage = True >>> histogram.bin_count = 20 >>> histogram.set_data([0, 1, 2, 3])
- _axes(painter: QPainter, left: int, top: int, right: int, bottom: int, x_min: float, x_max: float, y_max: float) None[source]#
Draw axes, tick marks, labels and axis titles.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries.
top (int) – Plotting area boundaries.
right (int) – Plotting area boundaries.
bottom (int) – Plotting area boundaries.
x_min (float) – Numerical X-axis range.
x_max (float) – Numerical X-axis range.
y_max (float) – Numerical Y-axis maximum.
- _bars(painter: QPainter, left: int, top: int, right: int, bottom: int, x_min: float, x_max: float, y_max: float, values: Sequence[float]) None[source]#
Draw histogram bars.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries.
top (int) – Plotting area boundaries.
right (int) – Plotting area boundaries.
bottom (int) – Plotting area boundaries.
x_min (float) – Numerical X-axis range.
x_max (float) – Numerical X-axis range.
y_max (float) – Maximum displayed Y value.
values (Sequence[float]) – Values to render for each histogram bin.
Notes
The bin boundaries in
self._edgesdetermine the horizontal position and width of each bar. This means non-uniform bins are rendered with their actual widths.The bar height is based on the transformed
valuesargument, so cumulative and percentage modes do not modify the underlying histogram data.
- _display_values() list[float][source]#
Return bin values transformed for the current display mode.
The original
self._binsvalues are never modified.Transformation order is:
Start with absolute bin values.
If cumulative mode is enabled, compute the cumulative sum.
3. If percentage mode is enabled, divide by the total of the original histogram and multiply by 100.
This ordering ensures that cumulative percentage mode reaches 100 percent at the final non-empty bin.
- Returns:
Values that should be rendered by the histogram.
- Return type:
list[float]
- static _draw_horizontal_grid(painter: QPainter, left: int, right: int, top: int, bottom: int, line_count: int, color: QColor, width: int) None[source]#
Draw evenly spaced horizontal grid lines.
The grid includes both the top and bottom boundaries of the plotting area. Therefore,
line_countrepresents the number of intervals, and the method drawsline_count + 1horizontal lines.The first line is drawn at the bottom of the plotting area and subsequent lines are drawn upward until the top boundary is reached.
For example, with
line_count=5the method draws six horizontal lines at positions corresponding to:0 %, 20 %, 40 %, 60 %, 80 %, 100 %
- Parameters:
painter (QPainter) – Painter used to draw the grid lines.
left (int) – X coordinate of the left side of the plotting area.
right (int) – X coordinate of the right side of the plotting area.
top (int) – Y coordinate of the top of the plotting area.
bottom (int) – Y coordinate of the bottom of the plotting area.
line_count (int) – Number of intervals between horizontal grid lines. Must be greater than zero.
color (QColor) – Color used for the grid lines.
width (int) – Width of the grid lines.
- Raises:
ValueError – If
line_countis not positive or ifbottomis smaller thantop.
- static _draw_vertical_grid(painter: QPainter, left: int, right: int, top: int, bottom: int, line_count: int, color: QColor, width: int) None[source]#
Draw evenly spaced vertical grid lines.
The grid includes both boundaries of the plotting area. Therefore,
line_countrepresents the number of intervals between the first and last grid line, and the method drawsline_count + 1lines.For example, with
line_count=5the method draws six vertical lines at positions corresponding to:0 %, 20 %, 40 %, 60 %, 80 %, 100 %
- Parameters:
painter (QPainter) – Painter used to draw the grid lines.
left (int) – X coordinate of the left side of the plotting area.
right (int) – X coordinate of the right side of the plotting area.
top (int) – Y coordinate of the top of the plotting area.
bottom (int) – Y coordinate of the bottom of the plotting area.
line_count (int) – Number of intervals between vertical grid lines. Must be greater than zero.
color (QColor) – Color used for the grid lines.
width (int) – Width of the grid lines.
- Raises:
ValueError – If
line_countis not positive or ifrightis smaller thanleft.
- _frame(painter: QPainter, left: int, top: int, right: int, bottom: int) None[source]#
Draw the border surrounding the plotting area.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries.
top (int) – Plotting area boundaries.
right (int) – Plotting area boundaries.
bottom (int) – Plotting area boundaries.
- _get_y_ticks(y_max: float) tuple[list[float], list[str], str][source]#
Generate Y-axis tick positions, labels and title.
Tick positions are kept at their exact numerical positions. Only their textual representation is rounded. This prevents the visual position of a tick from becoming inconsistent with the actual Y-axis scale.
- Parameters:
y_max (float) – Maximum value represented by the Y-axis.
- Returns:
A tuple containing the numerical tick positions, their formatted labels and the Y-axis title.
- Return type:
tuple[list[float], list[str], str]
- Raises:
ValueError – If
y_maxis negative or if_grid_yis not positive.
- _grid(painter: QPainter, left: int, top: int, right: int, bottom: int, y_max: float) None[source]#
Draw major and minor grid lines.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries in widget coordinates.
top (int) – Plotting area boundaries in widget coordinates.
right (int) – Plotting area boundaries in widget coordinates.
bottom (int) – Plotting area boundaries in widget coordinates.
y_max (float) – Maximum displayed Y value.
Notes
y_maxis currently not required because horizontal grid lines are evenly distributed geometrically. It is kept in the signature so the grid method can later be adapted to non-linear axes without changing its callers.
- _labels_x(painter: QPainter, left: int, right: int, bottom: int, x_min: float, x_max: float, ticks: Sequence[float]) None[source]#
Draw formatted X-axis labels.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries.
right (int) – Plotting area boundaries.
bottom (int) – Plotting area boundaries.
x_min (float) – Numerical X-axis range.
x_max (float) – Numerical X-axis range.
ticks (Sequence[float]) – X values whose labels should be drawn.
- _labels_y(painter: QPainter, left: int, top: int, bottom: int, y_max: float, ticks: Sequence[float], labels: Sequence[str]) None[source]#
Draw formatted Y-axis labels.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries.
top (int) – Plotting area boundaries.
bottom (int) – Plotting area boundaries.
y_max (float) – Numerical Y-axis maximum.
ticks (Sequence[float]) – Numerical Y positions corresponding to
labels.labels (Sequence[str]) – Text displayed for each Y-axis tick.
- Raises:
ValueError – If the number of labels differs from the number of ticks.
- _paint_histogram(painter: QPainter) None[source]#
Paint the histogram plotting area and its frame.
The method computes the displayed values from the original bins according to the current cumulative and percentage modes.
Empty histograms still display the plotting frame but do not draw grid lines, bars or axes based on data.
- _ticks_x(painter: QPainter, left: int, right: int, bottom: int, x_min: float, x_max: float, ticks: Sequence[float]) None[source]#
Draw X-axis tick marks.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries.
right (int) – Plotting area boundaries.
bottom (int) – Plotting area boundaries.
x_min (float) – Numerical X-axis range.
x_max (float) – Numerical X-axis range.
ticks (Sequence[float]) – X values at which ticks should be drawn.
- _ticks_y(painter: QPainter, left: int, top: int, bottom: int, y_max: float, ticks: Sequence[float]) None[source]#
Draw Y-axis tick marks.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries.
top (int) – Plotting area boundaries.
bottom (int) – Plotting area boundaries.
y_max (float) – Numerical Y-axis maximum.
ticks (Sequence[float]) – Y values at which ticks should be drawn.
- _titles(painter: QPainter, left: int, top: int, right: int, bottom: int, y_title: str) None[source]#
Draw the X-axis and Y-axis titles.
- Parameters:
painter (QPainter) – Painter used for rendering.
left (int) – Plotting area boundaries.
top (int) – Plotting area boundaries.
right (int) – Plotting area boundaries.
bottom (int) – Plotting area boundaries.
y_title (str) – Title of the Y-axis generated by
_get_y_ticks().
- static _validate_edges(edges: Sequence[float]) None[source]#
Validate histogram bin boundaries.
- Parameters:
edges (Sequence[float]) – Bin boundaries.
- Raises:
ValueError – If fewer than two boundaries are provided or if boundaries are not strictly increasing.
- property background_color#
- property bar_border_color#
- property bar_color#
- property bin_count: int#
Default number of bins used by
set_data().- Type:
int
- property bins: list[float]#
Copy of the original bin values.
- Type:
list[float]
- clear() None[source]#
Remove all histogram data.
The current display mode, axis configuration, colors and other visual properties are preserved.
- property cumulative: bool#
Whether cumulative values are displayed.
- Type:
bool
- property edges: list[float]#
Copy of the current bin boundaries.
- Type:
list[float]
- paintEvent(event) None[source]#
Paint the complete histogram widget.
- Parameters:
event (QPaintEvent) – Qt paint event requesting the widget to be repainted.
- property percentage: bool#
Whether values are displayed as percentages.
- Type:
bool
- set_bins(bins: Sequence[float], edges: Sequence[float] | None = None) None[source]#
Set precomputed histogram bin values.
The values are stored as absolute values. Display transformations such as cumulative values and percentages are applied only during painting.
- Parameters:
bins (Sequence[float]) – Non-negative values associated with each histogram bin.
edges (Sequence[float], optional) – Bin boundaries. There must be exactly
len(bins) + 1boundaries. If omitted, integer boundaries starting at zero are generated.
- Raises:
ValueError – If a bin value is negative, if the edges are invalid, or if the number of edges does not match the number of bins.
- set_cumulative(value: bool) None[source]#
Enable or disable cumulative mode.
- Parameters:
value (bool) – Whether cumulative values should be displayed.
- set_data(data: Sequence[float], bins: int | Sequence[float] | None = None) None[source]#
Build and store a histogram from raw numeric samples.
The input samples are converted to
floatbefore binning.If
binsis an integer, that value specifies the number of equally spaced bins between the minimum and maximum sample.If
binsis a sequence, it is interpreted as explicit bin boundaries. In this case the boundaries do not need to be equally spaced.The rightmost edge is inclusive, so a value exactly equal to
edges[-1]is assigned to the last bin.- Parameters:
data (Sequence[float]) – Raw numeric samples.
bins (int or Sequence[float], optional) – Number of equally spaced bins, or explicit bin boundaries.
- Raises:
ValueError – If the number of bins is not positive or if explicit bin boundaries are invalid.
- set_percentage(value: bool) None[source]#
Enable or disable percentage mode.
- Parameters:
value (bool) – Whether percentages should be displayed instead of absolute values.
- property show_axes: bool#
Whether axes are displayed.
- Type:
bool
- property show_grid: bool#
Whether major grid lines are displayed.
- Type:
bool
- property show_labels: bool#
Whether axis labels are displayed.
- Type:
bool
- property show_minor_grid: bool#
Whether minor grid lines are displayed.
- Type:
bool
- property show_ticks: bool#
Whether ticks are displayed.
- Type:
bool
- property x_formatter: Callable[[float], str]#
X-axis label formatter.
- Type:
Callable[[float], str]
- property x_ticks: Sequence[float] | None#
Explicit X-axis tick positions.
- Type:
Sequence[float] | None
- property x_title: str#
X-axis title.
- Type:
str