Histogram.plot#

Histogram.plot(type: Literal['frequency', 'pdf', 'probability', 'cf', 'cumulative', 'cdf', 'normalized', 'ncdf'] = 'frequency', block: bool = False, filled: bool | None = None, stats: bool = True, style: str = 'stack', cursor: bool = False, alpha: float = 0.5, title: str | None = None, log: bool = False, samescale: bool = False, ax: Axes | None = None, bar: bool | None = None, **kwargs: Any) None[source][source]#

Plot histogram

Parameters:
  • type – histogram type, one of: ‘frequency’ [default], ‘pdf’/’probability’, ‘cf’/’cumulative’, ‘cdf’/’normalized’; ‘ncdf’ is accepted as a deprecated alias for ‘cdf’

  • block (bool, optional) – hold plot, defaults to False

  • filled (bool, optional) – use a filled stairs plot, defaults to True for frequency plot, False for other plots

  • stats (bool, optional) – draw vertical lines for mean (solid) and median (dashed), defaults to True

  • style (str, optional) – Style for multiple plots, one of: ‘stack’ [default], ‘overlay’

  • cursor (bool, optional) – enable interactive data cursor for stacked line plots, defaults to False. Cursor is ignored for overlay style.

  • alpha (float, optional) – transparency for overlay plot, defaults to 0.5

  • title (str, optional) – plot title, defaults to None

  • log (bool, optional) – use logarithmic y-axis, defaults to False

  • samescale (bool, optional) – synchronize the y-axis scale for all subplots, defaults to True

  • ax (matplotlib.axes.Axes, optional) – Matplotlib axes to plot on, defaults to None (new figure)

  • bar – deprecated alias for filled, use filled= instead

  • kwargs – additional keyword arguments passed to Matplotlib plotting functions, plt.stairs for solid=True and plt.plot for solid=False, and plt.Polygon for style='overlay'

Raises:
  • ValueError – invalid histogram type

  • ValueError – cannot use overlay style for 1-channel histogram

Plots the histogram using Matplotlib. For a color image, the histograms of each plane are plotted separately. The type option selects the type of histogram to plot: frequency, pdf/probability, cf/cumulative, or cdf/normalized (normalized cumulative, range 0 to 1). ncdf is accepted as a deprecated alias for cdf.

The style option selects the style for plotting multiple planes:

  • stack: plot each plane in a separate subplot. The cursor option enables an interactive data cursor which displays the histogram values at the cursor position. The filled option selects whether to use a filled stairs plot.

  • overlay: plot all planes in the same axes with different colors. The filled option selects whether to use a filled plot. The alpha option controls the transparency of the bars in the ‘’overlay’’ style.

samescale controls whether the y-axis scale is the same for all planes (if False, each plane is scaled independently). By default, the y-axis scale is the same for all planes, which allows for direct comparison of histogram values across planes.

The log option uses a logarithmic scale for the y-axis, which can be useful for visualizing histograms with a large dynamic range, zero values are ignored in log plots.

If ax is provided, the histogram is plotted on the given Matplotlib axes. For multi-plane histograms the style is overridden to ‘overlay’ since all the plots must be on one axes. If ax is not provided, a new figure and axes are created.

>>> from machinevisiontoolbox import Image
>>> im = Image.Read('street.png')
>>> hist = im.hist()
>>> print(hist)
histogram with 256 bins: xrange 0 - 255, yrange 21 - 11429
>>> hist.plot() # standard frequency plot
>>> hist.plot(type='cdf') # CDF plot

(Source code, png, hires.png, pdf)

../../_images/machinevisiontoolbox-Histogram-plot-1.png

(Source code, png, hires.png, pdf)

../../_images/machinevisiontoolbox-Histogram-plot-2.png

For multi-plane (color) images, the histograms of each plane can be plotted separately in stacked subplots or overlaid on the same axes.

>>> from machinevisiontoolbox import Image
>>> im = Image.Read('flowers1.png')
>>> im.hist().plot(style='stack', filled=True)
>>> im.hist().plot(style='overlay', filled=True, alpha=0.5)

(Source code, png, hires.png, pdf)

../../_images/machinevisiontoolbox-Histogram-plot-3.png

(Source code, png, hires.png, pdf)

../../_images/machinevisiontoolbox-Histogram-plot-4.png