HuginSourcehugin: Functional Plotting and Visualization Library for OCaml
Provides composable artists, axes, figures, and high-level plotting APIs for creating scientific and statistical graphics using Nx.
Module containing functions to add standard plot types to an Axes.
These functions create a Figure and Axes implicitly, plot the data, apply common optional decorations, and return the Figure.
val plot :
?title:string ->
?xlabel:string ->
?ylabel:string ->
?color:Artist.color ->
?linewidth:float ->
?linestyle:Artist.line_style ->
?marker:Artist.marker_style ->
?label:string ->
Nx.float32_t ->
Nx.float32_t ->
figureplot ?title ?xlabel ?ylabel ?color ?linewidth ?linestyle ?marker ?label x y
Create a new figure and plot y versus x.
Parameters
?title: title displayed atop the figure.?xlabel: label for the x-axis.?ylabel: label for the y-axis.?color: line and marker color.?linewidth: thickness of the line.?linestyle: dash pattern of 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.Returns
figure containing the created plot.Examples
let fig = plot x_arr y_arr in
savefig fig "plot.png"val plot_y :
?title:string ->
?xlabel:string ->
?ylabel:string ->
?color:Artist.color ->
?linewidth:float ->
?linestyle:Artist.line_style ->
?marker:Artist.marker_style ->
?label:string ->
Nx.float32_t ->
figureplot_y ?title ?xlabel ?ylabel ?color ?linewidth ?linestyle ?marker ?label y
Create a new figure and plot the data y against indices 0..N-1.
Parameters
?title: figure title.?xlabel: x-axis label.?ylabel: y-axis label.?color: line/marker color.?linewidth: thickness of the line.?linestyle: dash pattern of the line.?marker: marker style at data points.?label: legend entry for the series.y: 1D float32 nx of y values.Returns
figure containing the plotted series.Examples
let fig = plot_y y_arr in
savefig fig "plot_y.png"val plot3d :
?title:string ->
?xlabel:string ->
?ylabel:string ->
?zlabel:string ->
?color:Artist.color ->
?linewidth:float ->
?linestyle:Artist.line_style ->
?marker:Artist.marker_style ->
?label:string ->
Nx.float32_t ->
Nx.float32_t ->
Nx.float32_t ->
figureplot3d ?title ?xlabel ?ylabel ?zlabel ?color ?linewidth ?linestyle ?marker ?label x y z
Create a new figure, add 3D axes, and plot a 3D line through points.
Parameters
?title: figure title.?xlabel, ?ylabel, ?zlabel: axis labels.?color: line/marker color.?linewidth: line width.?linestyle: dash pattern.?marker: marker style.?label: legend entry.x, y, z: 1D float32 nxs of coordinates.Returns
figure with the 3D line plotted.Examples
let fig = plot3d x_arr y_arr z_arr in
savefig fig "plot3d.png"val scatter :
?title:string ->
?xlabel:string ->
?ylabel:string ->
?s:float ->
?c:Artist.color ->
?marker:Artist.marker_style ->
?label:string ->
Nx.float32_t ->
Nx.float32_t ->
figurescatter ?title ?xlabel ?ylabel ?s ?c ?marker ?label x y
Create a new figure and scatter plot points (x,y).
Parameters
?title: figure title.?xlabel: x-axis label.?ylabel: y-axis label.?s: marker size in points.?c: marker color.?marker: marker style.?label: legend entry.x, y: coordinate arrays.Returns
figure containing the scatter plot.Raises
Invalid_argument if lengths of x and y differ.Examples
let fig = scatter x_arr y_arr in
savefig fig "scatter.png"val hist :
?bins:[ `Num of int | `Edges of float array ] ->
?range:(float * float) ->
?density:bool ->
?title:string ->
?xlabel:string ->
?ylabel:string ->
?color:Artist.color ->
?label:string ->
Nx.float32_t ->
figurehist ?bins ?range ?density ?title ?xlabel ?ylabel ?color ?label x
Create a new figure and plot a histogram of the data in x.
Parameters
?bins: number of bins or explicit edges.?range: (min, max) interval for data inclusion.?density: if true, plot probability density instead of counts.?title: figure title.?xlabel: x-axis label.?ylabel: y-axis label.?color: bar fill color.?label: legend entry.x: 1D float32 nx of data values.Returns
figure containing the histogram plot.Examples
let fig = hist ~bins:(`Num 50) ~range:(0., 1.) ~density:true data in
savefig fig "hist.png"val bar :
?width:float ->
?bottom:float ->
?title:string ->
?xlabel:string ->
?ylabel:string ->
?color:Artist.color ->
?label:string ->
height:Nx.float32_t ->
Nx.float32_t ->
figurebar ?width ?bottom ?title ?xlabel ?ylabel ?color ?label ~height x
Create a new figure and render a bar chart.
Parameters
?width: width of each bar (default 0.8).?bottom: baseline for bars (default 0.0).?title: figure title.?xlabel: label for x-axis.?ylabel: label for y-axis.?color: bar fill color.?label: legend entry.~height: 1D float32 nx of bar heights.x: 1D float32 nx of bar positions.Returns
figure containing the bar chart.Examples
let fig = bar ~height:h_arr x_arr in
savefig fig "bar.png"val imshow :
?cmap:Artist.cmap ->
?aspect:string ->
?extent:(float * float * float * float) ->
?title:string ->
?xlabel:string ->
?ylabel:string ->
('a, 'b) Nx.t ->
figureimshow ?cmap ?aspect ?extent ?title ?xlabel ?ylabel data
Create a new figure and display an image from a data array.
Parameters
?cmap: colormap for single-channel (2D) data.?aspect: aspect ratio mode ("auto" or "equal").?extent: bounding box (xmin, xmax, ymin, ymax).?title: figure title.?xlabel, ?ylabel: axis labels.data: nx of shape |H;W|, |H;W;3|, or |H;W;4|.Returns
figure with the image displayed.Raises
Invalid_argument if data rank or shape is unsupported.Examples
let fig = imshow img_arr in
savefig fig "image.png"val step :
?title:string ->
?xlabel:string ->
?ylabel:string ->
?color:Artist.color ->
?linewidth:float ->
?linestyle:Artist.line_style ->
?where:Artist.step_where ->
?label:string ->
Nx.float32_t ->
Nx.float32_t ->
figurestep ?title ?xlabel ?ylabel ?color ?linewidth ?linestyle ?where ?label x y
Create a new figure and draw a step plot for data y vs x.
Parameters
?title: figure title.?xlabel, ?ylabel: axis labels.?color: line color.?linewidth: line width.?linestyle: dash pattern.?where: step alignment relative to x.?label: legend entry.x, y: coordinate arrays.Returns
figure containing the step plot.Raises
Invalid_argument on length mismatch.Examples
let fig = step x_arr y_arr ~where:Mid in
savefig fig "step.png"val fill_between :
?title:string ->
?xlabel:string ->
?ylabel:string ->
?color:Artist.color ->
?where:Nx.float32_t ->
?interpolate:bool ->
?label:string ->
Nx.float32_t ->
Nx.float32_t ->
Nx.float32_t ->
figurefill_between ?title ?xlabel ?ylabel ?color ?where ?interpolate ?label x y1 y2
Create a new figure and shade the area between two curves.
Parameters
?title: figure title.?xlabel, ?ylabel: axis labels.?color: fill color.?where: mask array selecting regions to fill.?interpolate: interpolate across missing regions.?label: legend entry.x: x-coordinate array.y1, y2: arrays defining lower and upper curves.Returns
figure containing the shaded region.Raises
Invalid_argument on length mismatch.Examples
let fig = fill_between x_arr y_lo y_hi in
savefig fig "area.png"val errorbar :
?title:string ->
?xlabel:string ->
?ylabel:string ->
?yerr:Nx.float32_t ->
?xerr:Nx.float32_t ->
?ecolor:Artist.color ->
?elinewidth:float ->
?capsize:float ->
?fmt:Artist.plot_style ->
?label:string ->
Nx.float32_t ->
Nx.float32_t ->
figureerrorbar ?title ?xlabel ?ylabel ?yerr ?xerr ?ecolor ?elinewidth ?capsize ?fmt ?label x y
Create a new figure and plot data with error bars.
Parameters
?title: figure title.?xlabel, ?ylabel: axis labels.?yerr: array of y-error values.?xerr: array of x-error values.?ecolor: error bar color.?elinewidth: error bar line width.?capsize: cap size in points.?fmt: plot style for central points/line.?label: legend entry.x, y: data coordinate arrays.Returns
figure containing the error bar plot.Raises
Invalid_argument if array lengths mismatch.Examples
let fig = errorbar x_arr y_arr ~yerr:y_err in
savefig fig "errorbar.png"val matshow :
?cmap:Artist.cmap ->
?aspect:string ->
?extent:(float * float * float * float) ->
?origin:[ `upper | `lower ] ->
?title:string ->
('a, 'b) Nx.t ->
figurematshow ?cmap ?aspect ?extent ?origin ?title data
Create a new figure and display a 2D matrix with cell coloring.
Parameters
?cmap: colormap for mapping values.?aspect: aspect ratio mode.?extent: (xmin,xmax,ymin,ymax) plot bounds.?origin: `upper or `lower y-axis origin placement.?title: figure title.data: 2D nx of numeric values.Returns
figure containing the matrix image.Examples
let fig = matshow matrix in
savefig fig "matrix.png"figure ?width ?height ()
Create a new figure canvas with optional size.
Parameters
?width: width in pixels (default 800).?height: height in pixels (default 600).(): unit argument.Returns
figure.Examples
let fig = figure ~width:1024 ~height:768 () in
...val subplot :
?nrows:int ->
?ncols:int ->
?index:int ->
?projection:Axes.projection ->
figure ->
axessubplot ?nrows ?ncols ?index ?projection fig
Add a subplot in a grid layout to the figure.
Parameters
?nrows: number of rows (default 1).?ncols: number of columns (default 1).?index: subplot index (1-based, default 1).?projection: TwoD or ThreeD axes type.fig: parent figure.Returns
axes.Examples
let ax = subplot ~nrows:2 ~ncols:2 ~index:3 fig in
...val add_axes :
left:float ->
bottom:float ->
width:float ->
height:float ->
?projection:Axes.projection ->
figure ->
axesadd_axes ~left ~bottom ~width ~height ?projection fig
Add custom-positioned axes to the figure.
Parameters
~left: left margin (fraction of figure width).~bottom: bottom margin (fraction of figure height).~width, ~height: size of axes (fractions).?projection: TwoD or ThreeD.fig: parent figure.Returns
axes.Examples
let ax = add_axes ~left:0.1 ~bottom:0.1 ~width:0.8 ~height:0.8 fig in
...show figure
Render and display the figure in an interactive window.
Parameters
figure: the figure to display.Notes
Examples
let fig = plot x_arr y_arr in
show figsavefig ?dpi ?format filename fig
Save the figure to a file.
Parameters
?dpi: resolution in dots per inch (default 100).?format: output format (e.g., "png", "pdf"); inferred from extension by default.filename: destination file path.fig: figure to save.Notes
Examples
let fig = plot x_arr y_arr in
savefig ~dpi:300 "highres.png" figrender ?format fig
Render to an in-memory buffer and return the image data as a string.
Parameters
?format: output format (default "png").fig: figure to save.Returns
Notes
Examples
let fig = plot x_arr y_arr in
let png_data = save_to_buffer fig in
(* Use png_data, e.g., for network transmission or further processing *)