Documentation
¶
Overview ¶
Package plot draws publication-quality figures — line plots, scatter plots, bars, histograms, images, contours — to SVG and PNG, in pure Go.
The object model is matplotlib's: a Figure holds Axes, an Axes holds what is plotted on it, and the defaults (sizes, fonts, colours, margins, tick placement and labels) are matplotlib's own, so a figure looks the way a matplotlib user expects:
fig, axs := plot.Subplots(1, 1)
ax := axs[0][0]
ax.Plot(x, y, plot.Label("data"))
ax.SetXLabel("time $t$ (s)")
ax.Legend()
fig.SavePNG(w)
Labels may contain TeX math between dollar signs, typeset by go-tex/math.
Index ¶
- func Values(v any) []float64
- type Axes
- func (a *Axes) Annotate(s string, x, y, tx, ty float64, opts ...Option)
- func (a *Axes) Axhline(y float64, opts ...Option)
- func (a *Axes) Axvline(x float64, opts ...Option)
- func (a *Axes) Bar(x, height any, opts ...Option)
- func (a *Axes) Barh(y, width any, opts ...Option)
- func (a *Axes) Contour(x, y, z any, opts ...Option) *Mappable
- func (a *Axes) Contourf(x, y, z any, opts ...Option) *Mappable
- func (a *Axes) ErrorBar(x, y any, opts ...Option)
- func (a *Axes) Figure() *Figure
- func (a *Axes) FillBetween(x, y1, y2 any, opts ...Option)
- func (a *Axes) Grid(on bool)
- func (a *Axes) Hist(x any, opts ...Option)
- func (a *Axes) Imshow(data any, opts ...Option) *Mappable
- func (a *Axes) Legend(opts ...Option)
- func (a *Axes) Plot(x, y any, opts ...Option)
- func (a *Axes) Scatter(x, y any, opts ...Option) *Mappable
- func (a *Axes) SetAspect(r float64)
- func (a *Axes) SetTitle(s string)
- func (a *Axes) SetXLabel(s string)
- func (a *Axes) SetXLim(lo, hi float64)
- func (a *Axes) SetXScale(s string)
- func (a *Axes) SetXTicks(locs []float64, labels ...string)
- func (a *Axes) SetYLabel(s string)
- func (a *Axes) SetYLim(lo, hi float64)
- func (a *Axes) SetYScale(s string)
- func (a *Axes) SetYTicks(locs []float64, labels ...string)
- func (a *Axes) Text(x, y float64, s string, opts ...Option)
- func (a *Axes) TwinX() *Axes
- func (a *Axes) XLim() (float64, float64)
- func (a *Axes) YLim() (float64, float64)
- type FigOption
- type Figure
- func (f *Figure) AddAxes(left, bottom, width, height float64) *Axes
- func (f *Figure) Axes() []*Axes
- func (f *Figure) Colorbar(m *Mappable, ax *Axes) *Axes
- func (f *Figure) Display() map[string][]byte
- func (f *Figure) Draw(r render.Renderer)
- func (f *Figure) Image() *render.Raster
- func (f *Figure) PixelSize() (w, h int)
- func (f *Figure) SavePNG(w io.Writer) error
- func (f *Figure) SaveSVG(w io.Writer) error
- func (f *Figure) Subplots(rows, cols int) [][]*Axes
- func (f *Figure) TightLayout() bool
- type Mappable
- type Option
- func Align(a string) Option
- func Alpha(a float64) Option
- func Arrow() Option
- func Aspect(a string) Option
- func BinEdges(e ...float64) Option
- func Bins(n int) Option
- func Bottom(b float64) Option
- func CValues(v any) Option
- func CapSize(s float64) Option
- func Clip(on bool) Option
- func Cmap(name string) Option
- func Color(c string) Option
- func Colors(cs ...string) Option
- func Density() Option
- func EdgeColor(c string) Option
- func ErrorColor(c string) Option
- func Extent(left, right, bottom, top float64) Option
- func FaceColor(c string) Option
- func FontSize(s float64) Option
- func HA(a string) Option
- func Label(s string) Option
- func Levels(l ...float64) Option
- func LineStyle(s string) Option
- func LineWidth(w float64) Option
- func Loc(s string) Option
- func LogNorm() Option
- func Marker(m string) Option
- func MarkerEdgeWidth(w float64) Option
- func MarkerSize(s float64) Option
- func NLevels(n int) Option
- func Origin(o string) Option
- func RGBA(r, g, b, a float64) Option
- func Rotation(deg float64) Option
- func Sizes(v any) Option
- func VA(a string) Option
- func Vmax(v float64) Option
- func Vmin(v float64) Option
- func Where(mask []bool) Option
- func Width(w float64) Option
- func XErr(v any) Option
- func YErr(v any) Option
- func ZOrder(z float64) Option
- type SubplotParams
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Values ¶
Values converts plot data to []float64. It accepts []float64, []float32, []int, []int64, []int32, a single float64 or int, any *ndarray.Array (of any dtype, flattened in row-major order; complex values keep their real part, as NumPy's astype does) and nil (which gives nil). Anything else panics: passing a type a plot cannot draw is a programming error, like an index out of range.
Types ¶
type Axes ¶
Axes is one plotting area: two axes, the things plotted on them, a title and a legend. Create it with Figure.Subplots, Subplots or Figure.AddAxes.
func (*Axes) Annotate ¶
Annotate writes s at (tx, ty) and, with the Arrow option, draws an arrow from it to the point (x, y), as matplotlib's ax.annotate(s, xy=(x, y), xytext=(tx, ty), arrowprops=dict(arrowstyle="->")). Both points are in data coordinates.
Options: Arrow, and those of Text.
func (*Axes) Axhline ¶
Axhline draws a horizontal line across the Axes at y, as matplotlib's ax.axhline: it spans the full width whatever the x limits, and only y takes part in autoscaling. Its colour is C0 unless given; it does not advance the cycle.
Options: as Plot.
func (*Axes) Axvline ¶
Axvline draws a vertical line across the Axes at x, as matplotlib's ax.axvline.
Options: as Plot.
func (*Axes) Bar ¶
Bar draws vertical bars of the given heights at x, as matplotlib's ax.bar: 0.8 wide, centred on x, from 0, in the next colour of the fill cycle, with no edge.
Options: Width, Bottom, Align ("center" or "edge"), Color, Colors (one per bar), FaceColor, EdgeColor, LineWidth (edge), Alpha, Label, ZOrder.
func (*Axes) Barh ¶
Barh draws horizontal bars of the given widths at y, as matplotlib's ax.barh. Width sets the bar thickness (matplotlib's height=) and Bottom the left end.
Options: as Bar.
func (*Axes) Contour ¶
Contour draws iso-lines of z, as matplotlib's ax.contour: about 7 levels chosen like axis ticks, each coloured from the colormap over the levels' range. x and y give the column and row coordinates and may be nil.
Options: Levels, NLevels, Cmap, Vmin, Vmax, Color (all lines), LineWidth, Alpha, ZOrder.
func (*Axes) Contourf ¶
Contourf fills the bands between levels of z, as matplotlib's ax.contourf; each band takes the colour of its middle value.
Options: as Contour.
func (*Axes) ErrorBar ¶
ErrorBar draws y against x with error bars, as matplotlib's ax.errorbar: a line in the next cycle colour, and symmetric bars of YErr and/or XErr (one value for all points, or one per point) in the same colour, with caps CapSize points long on each side if CapSize is given.
Options: XErr, YErr, CapSize, ErrorColor, and those of Plot.
func (*Axes) FillBetween ¶
FillBetween fills the region between y1 and y2 over x, as matplotlib's ax.fill_between. y2 may be nil (zero) or data. Without a colour it takes the next colour of the fill cycle.
Options: Where, Color, FaceColor, EdgeColor, LineWidth, Alpha, Label, ZOrder.
func (*Axes) Hist ¶
Hist draws a histogram of x, as matplotlib's ax.hist with histtype "bar": 10 equal bins over the data range by default, touching bars in the next colour of the line cycle (as in matplotlib).
Options: Bins, BinEdges, Density, Color, Alpha, EdgeColor, LineWidth, Label, ZOrder.
func (*Axes) Imshow ¶
Imshow shows 2-D data as an image, as matplotlib's ax.imshow: each value coloured through the colormap (viridis by default, scaled to the data range), pixel centres at integer coordinates, row 0 at the top, square pixels. A 3-D *ndarray.Array of shape (rows, cols, 3 or 4) is shown as RGB(A): floats in [0, 1], or uint8.
Options: Cmap, Vmin, Vmax, LogNorm, Extent, Origin, Aspect, Alpha, ZOrder.
func (*Axes) Legend ¶
Legend adds a legend of every labelled artist, as matplotlib's ax.legend(): 10-point labels in a rounded, translucent frame.
Options: Loc, FontSize.
func (*Axes) Plot ¶
Plot draws y against x as lines and/or markers, as matplotlib's ax.plot does. x may be nil, meaning 0, 1, 2, ... . Without a colour option the line takes the next colour of the cycle.
Options: Color, RGBA, Alpha, LineWidth, LineStyle, Marker, MarkerSize, MarkerEdgeWidth, FaceColor (marker fill), EdgeColor (marker edge), Label, ZOrder, Clip.
func (*Axes) Scatter ¶
Scatter draws a marker at each (x, y), as matplotlib's ax.scatter: circles of 36 points² in the next colour of the fill cycle, edged in their own colour. With CValues the colours come from a colormap, and the returned Mappable can be given to Colorbar; otherwise it is nil.
Options: Sizes, CValues, Cmap, Vmin, Vmax, LogNorm, Color, Colors, Marker, EdgeColor, LineWidth (edge), Alpha, Label, ZOrder.
func (*Axes) SetAspect ¶
SetAspect sets the ratio of y units to x units on screen: 1 for "equal", 0 for "auto". The Axes box shrinks to honour it, as with matplotlib's adjustable="box".
func (*Axes) SetXLim ¶
SetXLim fixes the x view limits and turns x autoscaling off. Equal limits are widened, as matplotlib widens them.
func (*Axes) SetXTicks ¶
SetXTicks fixes the x tick locations and, when labels are given, their labels.
func (*Axes) SetYTicks ¶
SetYTicks fixes the y tick locations and, when labels are given, their labels.
func (*Axes) Text ¶
Text writes s at (x, y) in data coordinates, as matplotlib's ax.text: 10 points, black, left-aligned on its baseline, not clipped. s may contain TeX math between dollar signs.
Options: FontSize, Color, Alpha, HA, VA, Rotation, ZOrder.
func (*Axes) TwinX ¶
TwinX returns a new Axes sharing this one's x axis, with its y axis on the right, as matplotlib's ax.twinx() does. It has its own colour cycle, which starts again at C0, as in matplotlib.
type Figure ¶
type Figure struct {
// Width and Height are in inches; DPI is pixels per inch. The pixel size
// is Width*DPI x Height*DPI, rounded as matplotlib rounds it.
Width, Height, DPI float64
// Facecolor is the background; white by default.
Facecolor colors.RGBA
// Text lays out every string in the figure. The default sets text in
// DejaVu Sans and TeX math with go-tex/math; replace it to change fonts.
Text text.Renderer
// SubplotParams place the grids made by Subplots; TightLayout sets them.
SubplotParams SubplotParams
// contains filtered or unexported fields
}
Figure is the whole drawing: a size, a resolution, a background and the Axes on it.
func NewFigure ¶
NewFigure returns an empty figure with matplotlib's defaults: 6.4 x 4.8 inches at 100 dpi on white.
func Subplots ¶
Subplots returns a new figure with a rows x cols grid of Axes, as matplotlib's plt.subplots does; axs[i][j] is row i, column j.
func (*Figure) AddAxes ¶
AddAxes adds an Axes at [left, bottom, width, height] in figure fractions, as matplotlib's fig.add_axes does.
func (*Figure) Colorbar ¶
Colorbar adds a colour scale for m beside ax, taking its space from ax as matplotlib's fig.colorbar(m, ax=ax) does: 15% of the width, a 5% gap, a bar 20 times as tall as wide, ticks on the right. It returns the colorbar's Axes, whose y label can be set.
func (*Figure) Display ¶
Display returns the figure in every format a notebook front end may show, keyed by MIME type: "image/svg+xml" and "image/png". It is the display contract of the go-notebook kernels, which show the richest format they support.
func (*Figure) Subplots ¶
Subplots adds a rows x cols grid of Axes placed as matplotlib's GridSpec places them.
func (*Figure) TightLayout ¶
TightLayout moves the grid made by Subplots so that every Axes' tick labels, axis labels and title fit in the figure and do not overlap their neighbours, as matplotlib's fig.tight_layout() does: a pad of 1.08 times the font size (10 points) at the figure edges and between cells. Axes added with AddAxes are left where they are; an Axes with a colorbar counts as one cell with it.
It reports false, changing nothing, when the decorations cannot fit (or there is no grid), as matplotlib then warns and does nothing.
type Mappable ¶
type Mappable struct {
Cmap *colors.Colormap
Norm colors.Norm
// contains filtered or unexported fields
}
Mappable is what maps data values to colours — the result of Imshow, Scatter with CValues, Contour and Contourf — and what Colorbar draws.
type Option ¶
type Option func(*props)
An Option sets one property of what a plotting method creates, as a matplotlib keyword argument does: ax.Plot(x, y, plot.Color("C1"), plot.LineStyle("--"), plot.Label("data")). Each method documents the options it honours and ignores the rest.
func Arrow ¶
func Arrow() Option
Arrow draws an arrow from an annotation's text to the point it annotates.
func CValues ¶
CValues gives Scatter one value per point, mapped to colours through the colormap (matplotlib's c= with numbers). Any data Values accepts.
func Color ¶
Color sets the colour: a name ("red", "C1", "tab:blue"), a hex string or a grey level, as matplotlib accepts. An invalid colour panics.
func ErrorColor ¶
ErrorColor sets the colour of error bars (matplotlib's ecolor).
func Loc ¶
Loc sets where Legend goes: "best" (the default: the place covering the least data, as matplotlib chooses it) or one of matplotlib's location names ("upper right", "lower center", ...).
func Marker ¶
Marker sets the marker: "o", ".", ",", "s", "^", "v", "<", ">", "D", "d", "p", "h", "*", "+", "x", "|", "_", or "" for none.
func MarkerEdgeWidth ¶
MarkerEdgeWidth sets the marker edge width in points.
func Sizes ¶
Sizes gives Scatter one marker area per point, in points squared (matplotlib's s=). A single value applies to every point.
func VA ¶
VA sets vertical text alignment: "top", "center", "baseline", "center_baseline" or "bottom".
type SubplotParams ¶
SubplotParams are matplotlib's figure.subplot.* parameters: where the grid of Subplots sits in the figure (fractions) and the space between its cells (fractions of the average cell width and height).
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package colors parses colour specifications and maps scalars to colours the way matplotlib does: the same names, the same default property cycle (tab10), and colormaps whose lookup tables are matplotlib's own, generated from it by tools/gen_colormaps.py.
|
Package colors parses colour specifications and maps scalars to colours the way matplotlib does: the same names, the same default property cycle (tab10), and colormaps whose lookup tables are matplotlib's own, generated from it by tools/gen_colormaps.py. |
|
Package contour traces iso-lines and fills iso-bands of a function sampled on a rectangular grid, by marching squares with linear interpolation along cell edges.
|
Package contour traces iso-lines and fills iso-bands of a function sampled on a rectangular grid, by marching squares with linear interpolation along cell edges. |
|
Package render is the drawing seam between the figure model and its output formats.
|
Package render is the drawing seam between the figure model and its output formats. |
|
Package text turns label strings into glyph outlines.
|
Package text turns label strings into glyph outlines. |
|
Package ticker chooses where the ticks of an axis go and how they are labelled.
|
Package ticker chooses where the ticks of an axis go and how they are labelled. |
