Module Hugin.Plotting

Module containing functions to add standard plot types to an Axes.

val plot : ?color:Artist.color -> ?linewidth:float -> ?linestyle:Artist.line_style -> ?marker:Artist.marker_style -> ?label:string -> x:Nx.float32_t -> y:Nx.float32_t -> Axes.t -> Axes.t

plot ?color ?linewidth ?linestyle ?marker ?label ~x ~y axes

Plot y versus x on the specified axes.

Parameters

  • ?color: line/marker color.
  • ?linewidth: width of the line in points.
  • ?linestyle: dash pattern for the line.
  • ?marker: marker style at data points.
  • ?label: legend entry for the plotted data.
  • x: 1D float32 nx of x coordinates.
  • y: 1D float32 nx of y coordinates.
  • axes: target axes to draw the plot on.

Returns

  • updated axes with the plotted line and markers.

Raises

  • Invalid_argument if x and y lengths differ.

Examples

  let ax = plot x_arr y_arr ax in
  ...
val plot_y : ?color:Artist.color -> ?linewidth:float -> ?linestyle:Artist.line_style -> ?marker:Artist.marker_style -> ?label:string -> y:Nx.float32_t -> Axes.t -> Axes.t

plot_y ?color ?linewidth ?linestyle ?marker ?label ~y axes

Plot y data against integer indices 0..N-1 on the given axes.

Parameters

  • ?color: line/marker color.
  • ?linewidth: line width.
  • ?linestyle: dash pattern.
  • ?marker: point marker style.
  • ?label: legend entry.
  • y: 1D float32 nx of y values.
  • axes: target axes.

Returns

  • axes with the plotted data.

Examples

  let ax = plot_y y_arr ax in
  ...
val plot3d : ?color:Artist.color -> ?linewidth:float -> ?linestyle:Artist.line_style -> ?marker:Artist.marker_style -> ?label:string -> x:Nx.float32_t -> y:Nx.float32_t -> z:Nx.float32_t -> Axes.t -> Axes.t

plot3d ?color ?linewidth ?linestyle ?marker ?label ~x ~y ~z axes

Plot a 3D line through points (x.i, y.i, z.i).

Parameters

  • ?color: line/marker color.
  • ?linewidth: line thickness.
  • ?linestyle: dash pattern.
  • ?marker: marker style.
  • ?label: legend entry.
  • x, y, z: 1D float32 nxs of coordinates.
  • axes: 3D axes for plotting.

Returns

  • axes with the 3D line artist added.

Raises

  • Invalid_argument if lengths of x, y, z mismatch.

Examples

  let ax3d = plot3d x_arr y_arr z_arr ax3d in
  ...
val scatter : ?s:float -> ?c:Artist.color -> ?marker:Artist.marker_style -> ?label:string -> x:Nx.float32_t -> y:Nx.float32_t -> Axes.t -> Axes.t

scatter ?s ?c ?marker ?label ~x ~y axes

Create a scatter plot of points (x, y) on the axes.

Parameters

  • ?s: marker size.
  • ?c: marker color.
  • ?marker: style of marker.
  • ?label: legend label.
  • x, y: coordinate arrays.
  • axes: target axes.

Returns

  • axes with scatter artist added.

Raises

  • Invalid_argument on length mismatch.

Examples

  let ax = scatter x_arr y_arr ax in
  ...
val bar : ?width:float -> ?bottom:float -> ?color:Artist.color -> ?label:string -> x:Nx.float32_t -> height:Nx.float32_t -> Axes.t -> Axes.t

bar ?width ?bottom ?color ?label ~x ~height axes

Create a bar chart with bars centered at x and heights specified.

Parameters

  • ?width: bar width.
  • ?bottom: baseline for bars.
  • ?color: bar fill color.
  • ?label: legend label.
  • x: positions for bars.
  • height: heights of bars.
  • axes: target axes.

Returns

  • axes with bar artists added.

Raises

  • Invalid_argument if x and height lengths differ.

Examples

  let ax = bar ~x x_arr ~height h_arr ax in
  ...
val hist : ?bins:[ `Num of int | `Edges of float array ] -> ?range:(float * float) -> ?density:bool -> ?color:Artist.color -> ?label:string -> x:Nx.float32_t -> Axes.t -> Axes.t

hist ?bins ?range ?density ?color ?label ~x axes

Plot a histogram of the data in x.

Parameters

  • ?bins: number of bins or explicit edges.
  • ?range: (min, max) data range to include.
  • ?density: plot probability density instead of counts.
  • ?color: fill color for bars.
  • ?label: legend label.
  • x: 1D float32 array of data values.
  • axes: target axes.

Returns

  • axes with histogram bars added.

Raises

  • Invalid_argument on invalid range or bins.

Examples

  let ax = hist ~bins:(`Num 20) ~range:(0.,1.) ~density:true ~x x_arr ax in
  ...
val step : ?color:Artist.color -> ?linewidth:float -> ?linestyle:Artist.line_style -> ?where:Artist.step_where -> ?label:string -> x:Nx.float32_t -> y:Nx.float32_t -> Axes.t -> Axes.t

step ?color ?linewidth ?linestyle ?where ?label ~x ~y axes

Create a step plot connecting points with horizontal segments.

Parameters

  • ?color: line color.
  • ?linewidth: line thickness.
  • ?linestyle: dash pattern.
  • ?where: step alignment (Pre, Post, Mid).
  • ?label: legend entry.
  • x, y: 1D float32 arrays of coordinates.
  • axes: target axes.

Returns

  • axes with step plot added.

Raises

  • Invalid_argument if x and y lengths differ.

Examples

  let ax = step x_arr y_arr ~where:Mid ax in
  ...
val fill_between : ?color:Artist.color -> ?where:Nx.float32_t -> ?interpolate:bool -> ?label:string -> x:Nx.float32_t -> y1:Nx.float32_t -> y2:Nx.float32_t -> Axes.t -> Axes.t

fill_between ?color ?where ?interpolate ?label ~x ~y1 ~y2 axes

Shade region between curves y1(x) and y2(x).

Parameters

  • ?color: fill color.
  • ?where: boolean mask array; optional.
  • ?interpolate: fill missing segments (default false).
  • ?label: legend entry.
  • x, y1, y2: coordinate arrays.
  • axes: target axes.

Returns

  • axes with shaded region added.

Raises

  • Invalid_argument if lengths of x, y1, y2 mismatch.

Examples

  let ax = fill_between ~x x_arr ~y1 y_low ~y2 y_high ax in
  ...
val errorbar : ?yerr:Nx.float32_t -> ?xerr:Nx.float32_t -> ?ecolor:Artist.color -> ?elinewidth:float -> ?capsize:float -> ?fmt:Artist.plot_style -> ?label:string -> x:Nx.float32_t -> y:Nx.float32_t -> Axes.t -> Axes.t

errorbar ?yerr ?xerr ?ecolor ?elinewidth ?capsize ?fmt ?label ~x ~y axes

Add error bars to data points on the axes.

Parameters

  • ?yerr: symmetric y-error values.
  • ?xerr: symmetric x-error values.
  • ?ecolor: error bar line color.
  • ?elinewidth: error bar line width.
  • ?capsize: width of error bar caps in points.
  • ?fmt: central line/marker style as plot_style.
  • ?label: legend entry for central data.
  • x, y: data coordinate arrays.
  • axes: target axes.

Returns

  • axes with error bar artists added.

Raises

  • Invalid_argument if array lengths mismatch.

Examples

  let ax = errorbar x_arr y_arr ~yerr y_err ax in
  ...
val imshow : ?cmap:Artist.cmap -> ?aspect:string -> ?extent:(float * float * float * float) -> data:('a, 'b) Nx.t -> Axes.t -> Axes.t

imshow ?cmap ?aspect ?extent ~data axes

Display a 2D or 3D image array on the axes.

Parameters

  • ?cmap: colormap for single-channel arrays.
  • ?aspect: aspect ratio option ("auto" or "equal").
  • ?extent: (xmin,xmax,ymin,ymax) image bounds.
  • data: nx of shape |H;W|, |H;W;3|, or |H;W;4|.
  • axes: target axes.

Returns

  • axes with image artist.

Raises

  • Invalid_argument on unsupported array rank or shape.

Examples

  let ax = imshow img_arr ax in
  ...
val matshow : ?cmap:Artist.cmap -> ?aspect:string -> ?extent:(float * float * float * float) -> ?origin:[ `upper | `lower ] -> data:('a, 'b) Nx.t -> Axes.t -> Axes.t

matshow ?cmap ?aspect ?extent ?origin ~data axes

Display a 2D numeric matrix with grid-aligned cells.

Parameters

  • ?cmap: colormap for mapping values to colors.
  • ?aspect: aspect ratio for display.
  • ?extent: (xmin,xmax,ymin,ymax) bounds.
  • ?origin: data origin placement (`upper or `lower).
  • data: 2D nx of numeric values.
  • axes: target axes.

Returns

  • axes with matrix display.

Examples

  let ax = matshow ~data m_arr ax in
  ...
val text : ?color:Artist.color -> ?fontsize:float -> x:float -> y:float -> string -> Axes.t -> Axes.t

text ?color ?fontsize ~x ~y content axes

Add a text annotation at data coordinates.

Parameters

  • ?color: text color (default black).
  • ?fontsize: text size in points (default 12.0).
  • x: x-coordinate.
  • y: y-coordinate.
  • content: text string to display.
  • axes: target axes.

Returns

  • axes with text annotation added.

Examples

  let ax = text ~x:1. ~y:2. "Label" ax in
  ...
val contour : ?colors:Artist.color array -> ?linewidths:float array -> ?linestyles:Artist.line_style array -> levels:float array -> x:Nx.float32_t -> y:Nx.float32_t -> z:Nx.float32_t -> Axes.t -> Axes.t

contour ?colors ?linewidths ?linestyles ~levels ~x ~y ~z axes

Add contour lines to axes.

Parameters

  • ?colors: colors for each level.
  • ?linewidths: line widths for each level.
  • ?linestyles: line styles for each level.
  • levels: z values at which to draw contours.
  • x: 1D array of x coordinates.
  • y: 1D array of y coordinates.
  • z: 2D array of z values.
  • axes: target axes.

Returns

  • axes with contour lines added.

Examples

  let ax = contour ~levels:[|0.0; 1.0|] ~x ~y ~z ax in
  ...
val contourf : ?cmap:Artist.cmap -> ?alpha:float -> levels:float array -> x:Nx.float32_t -> y:Nx.float32_t -> z:Nx.float32_t -> Axes.t -> Axes.t

contourf ?cmap ?alpha ~levels ~x ~y ~z axes

Add filled contours to axes.

Parameters

  • ?cmap: colormap (default Viridis).
  • ?alpha: transparency (0.0 to 1.0, default 1.0).
  • levels: z values defining contour boundaries.
  • x: 1D array of x coordinates.
  • y: 1D array of y coordinates.
  • z: 2D array of z values.
  • axes: target axes.

Returns

  • axes with filled contours added.

Examples

  let ax = contourf ~cmap:Coolwarm ~levels:[|0.0; 1.0|] ~x ~y ~z ax in
  ...