gonum/plot

repository·master·Indexed 25 days ago

https://github.com/gonum/plot

An official Go library for building and drawing various types of plots, forked from the plotinum project. It provides a core API for plot layout, a set of standard plotter implementations for lines, scatter plots, box plots, and error bars, and a generic vector graphics API (vg) supporting back-ends such as EPS, PDF, PNG, and SVG. The library includes specialized packages for plotting (plot), standard chart types (plotter), and utility routines (plotutil).

Tokens
2.4K
Snippets
2
Records
25
Agent score
84%

What's inside gonum/plot

  1. Overview of gonum/plot packages

    master

    The gonum/plot library is organized into several specialized packages:

    • plot: Provides the core interface for laying out a plot and the primitives required for drawing.
    • plotter: Contains a standard set of Plotter implementations for common chart types like lines, scatter plots, box plots, and error bars.
    • plotutil: Provides utility routines for creating common plot types easily (note: this package is newer and subject to change).
    • vg: A generic vector graphics API that supports various back-ends such as EPS, draw2d, SVGo, X-Window, gopdf, and Gio.
  2. Create and configure a Legend

    master

    Use the Legend type to provide a description of different data elements in a plot. You can customize its appearance, position, and spacing.

    Configuration Options:

    • TextStyle: A text.Style defining the font and text properties for legend entries.
    • Padding: The vg.Length of space between entries. If zero, spacing is based on font size.
    • Top: If true, the legend is placed at the top edge; otherwise, it is at the bottom.
    • Left: If true, the legend is placed on the left edge with text following the icons. If false, it is on the right edge with text preceding the icons.
    • XOffs, YOffs: vg.Length offsets added to the final position.
    • YPosition: Vertical position of entries within their space. Valid values are [-1, +1], where +1 is the top and -1 is the bottom (use draw.PosBottom or draw.PosTop constants).
    • ThumbnailWidth: The vg.Length width of the icons/thumbnails.
  3. Add data to a plot using Add()

    master
    Use Add(ps ...Plotter) to add one or more Plotter implementations to your plot. If the provided plotters implement the DataRanger interface, the plot's X and Y axis ranges will automatically adjust to fit the data's minimum and maximum values.
  4. Configure a nominal X axis

    master
    Use NominalX(names ...string) to configure the X axis to use names (categorical data) instead of numbers. The names are mapped to integer locations (e.g., the first name is at $x=0$, the second at $x=1$, etc.).
  5. Hide axes using HideX, HideY, or HideAxes

    master

    You can remove axes from the plot using these methods:

    • HideX(): Removes the X axis (ticks, labels, and width).
    • HideY(): Removes the Y axis (ticks, labels, and width).
    • HideAxes(): Removes both X and Y axes.
  6. Align tiled plots using Align()

    master

    The Align function produces a two-dimensional row-major array of draw.Canvas objects. These canvases are designed to create tiled plots where the DataCanvases (the actual plotting areas) are evenly sized and spaced, regardless of the individual plot dimensions.

    Arguments:

    • plots [][]*Plot: A two-dimensional row-major array of *Plot objects.
    • t draw.Tiles: A tile configuration defining rows, columns, and padding.
    • dc draw.Canvas: The target canvas on which the tiled plots will be drawn.

    Returns:

    • [][]draw.Canvas: A two-dimensional array of canvases corresponding to the input plots structure, adjusted for alignment.

    Note: The function will panic if the dimensions of the plots array do not match the Rows and Cols specified in the draw.Tiles configuration.

    func Align(plots [][]*Plot, t draw.Tiles, dc draw.Canvas) [][]draw.Canvas
  7. Check the Gonum/plot version and checksum

    master
    Use the Version() function to retrieve the current version string and its corresponding checksum. Note that these values are only valid in binaries built with Go module support. If a replace directive is present in the go.mod file, the version string will reflect the replacement in the format version=>[replace-path] [replace-version].
  8. Generate optimal labels with talbotLinHanrahan

    master

    The talbotLinHanrahan function implements the Talbot, Lin and Hanrahan algorithm to return an optimal set of approximately want label values for a given data range [dMin, dMax]. It calculates the step and magnitude of the step between values.

    Parameters:

    • dMin, dMax (float64): The data range boundaries.
    • want (int): The desired number of labels.
    • containment (int): Use one of the containment constants (free, containData, or withinData).
    • Q ([]float64, optional): A slice of "nice numbers" used for tuning. If nil, defaults to []float64{1, 5, 2, 2.5, 4, 3}.
    • w (*weights, optional): Weights for simplicity, coverage, density, and legibility. If nil, default weights are used.
    • legibility (func(lMin, lMax, lStep float64) float64, optional): A function to tune the legibility assessment. If nil, defaults to unitLegibility (which returns 1).

    Returns:

    • values ([]float64): The generated label values.
    • step (float64): The step between values.
    • q (float64): The chosen q value.
    • magnitude (int): The magnitude of the label step distance.
  9. Use different scaling modes on an Axis

    master

    You can change how data values are mapped to the axis coordinate system by assigning a Normalizer to the Axis.Scale field.

    Supported scales:

    • LinearScale{}: Standard linear mapping.
    • LogScale{}: Logarithmic mapping. Note: All values (min, max, and x) must be greater than 0, otherwise it will panic.
    • InvertedScale{ Normalizer }: Inverts the axis using the provided Normalizer (e.g., wrapping a LinearScale to invert it).