Help us shape the future of Go! Take the 2026 Go Developer Survey

dfplot

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package dfplot draws DataFrames and Series, in the manner of the plot helpers of pandas (df.plot.line, df.plot.bar, ...) that notebooks lean on.

Every function returns a *plot.Figure from github.com/go-plot/plot, which can be displayed in a notebook (Figure.Display) or saved (SavePNG, SaveSVG). The index of the frame is the x axis:

  • a numeric index gives the x values themselves;
  • a datetime index gives numeric x values (days since the Unix epoch) with readable date tick labels;
  • any other index (strings, categories, several levels) places the points at positions 0, 1, 2, ... and shows the labels as ticks.

Missing values are not drawn: a line is broken where a value is missing (as matplotlib breaks it at a NaN), a bar is left out, and a missing value does not enter a histogram.

Example
package main

import (
	"bytes"
	"fmt"

	"github.com/go-dataframe/dataframe"
	"github.com/go-dataframe/dataframe/dfplot"
)

func main() {
	df := dataframe.Must(dataframe.New(
		dataframe.Col("day", []int64{1, 2, 3, 4}),
		dataframe.Col("sales", []float64{10, 12, 9, 15}),
		dataframe.Col("costs", []float64{8, 9, 9, 10}),
	))
	df, _ = df.SetIndex("day")

	fig, err := dfplot.Line(df, "sales", "costs")
	if err != nil {
		fmt.Println(err)
		return
	}
	var png bytes.Buffer
	if err := fig.SavePNG(&png); err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("axes:", len(fig.Axes()), "png:", png.Len() > 0)

	// Other kinds: dfplot.Bar, dfplot.Barh, dfplot.Hist, dfplot.Area,
	// dfplot.Scatter and the Series variants.
	_, err = dfplot.Scatter(df, "sales", "costs", dfplot.WithTitle("sales vs costs"))
	fmt.Println("scatter error:", err)
}
Output:
axes: 1 png: true
scatter error: <nil>

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoData = errors.New("dfplot: no data to plot")

ErrNoData reports a plot with nothing to draw: a nil frame, or a frame with no numeric column when none was named.

Functions

func Area

func Area(df *dataframe.DataFrame, columns ...string) (*plot.Figure, error)

Area draws the columns as stacked filled areas against the index, as df.plot.area() does; a missing value counts as zero.

func AreaOpts

func AreaOpts(df *dataframe.DataFrame, columns []string, opts []Option) (*plot.Figure, error)

AreaOpts is Area with options.

func Bar

func Bar(df *dataframe.DataFrame, columns ...string) (*plot.Figure, error)

Bar draws grouped vertical bars, one group per index label and one bar per column, as df.plot.bar() does. The labels are the x ticks. A missing value leaves its bar out.

func BarOpts

func BarOpts(df *dataframe.DataFrame, columns []string, opts []Option) (*plot.Figure, error)

BarOpts is Bar with options.

func Barh

func Barh(df *dataframe.DataFrame, columns ...string) (*plot.Figure, error)

Barh is Bar with horizontal bars: the index labels run up the y axis.

func BarhOpts

func BarhOpts(df *dataframe.DataFrame, columns []string, opts []Option) (*plot.Figure, error)

BarhOpts is Barh with options.

func Hist

func Hist(df *dataframe.DataFrame, bins int, columns ...string) (*plot.Figure, error)

Hist draws one histogram per column, overlaid with transparency and sharing the same bins, as df.plot.hist() does. bins is the number of bins (10 when it is not positive). The y label is "Frequency".

func HistOpts

func HistOpts(df *dataframe.DataFrame, bins int, columns []string, opts []Option) (*plot.Figure, error)

HistOpts is Hist with options.

func Line

func Line(df *dataframe.DataFrame, columns ...string) (*plot.Figure, error)

Line draws one line per column against the index, as df.plot() does in pandas. Without column names every numeric column is drawn. The legend names the columns and the x label is the name of the index. A missing value breaks the line.

func LineOpts

func LineOpts(df *dataframe.DataFrame, columns []string, opts []Option) (*plot.Figure, error)

LineOpts is Line with options; the columns are given as a slice.

func Scatter

func Scatter(df *dataframe.DataFrame, x, y string, opts ...Option) (*plot.Figure, error)

Scatter draws the column y against the column x, with both columns as the axis labels. Rows where either value is missing are skipped.

func SeriesBar

func SeriesBar(s *dataframe.Series, opts ...Option) (*plot.Figure, error)

SeriesBar draws a series as one bar per index label.

func SeriesHist

func SeriesHist(s *dataframe.Series, bins int, opts ...Option) (*plot.Figure, error)

SeriesHist draws the histogram of a series.

func SeriesLine

func SeriesLine(s *dataframe.Series, opts ...Option) (*plot.Figure, error)

SeriesLine draws a series against its index. It has no legend unless WithLegend(true) is given.

Types

type Option

type Option func(*config)

An Option adjusts a plot.

func WithGrid

func WithGrid(on bool) Option

WithGrid turns the grid on or off (off by default).

func WithLegend

func WithLegend(on bool) Option

WithLegend turns the legend on or off. A frame plot has one by default, a series plot has none.

func WithSize

func WithSize(width, height float64) Option

WithSize sets the figure size in inches (the default is 6.4 x 4.8).

func WithTitle

func WithTitle(s string) Option

WithTitle sets the title of the axes.

func WithXLabel

func WithXLabel(s string) Option

WithXLabel sets the label of the x axis; by default it is the name of the index.

func WithYLabel

func WithYLabel(s string) Option

WithYLabel sets the label of the y axis.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL