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 ¶
- Variables
- func Area(df *dataframe.DataFrame, columns ...string) (*plot.Figure, error)
- func AreaOpts(df *dataframe.DataFrame, columns []string, opts []Option) (*plot.Figure, error)
- func Bar(df *dataframe.DataFrame, columns ...string) (*plot.Figure, error)
- func BarOpts(df *dataframe.DataFrame, columns []string, opts []Option) (*plot.Figure, error)
- func Barh(df *dataframe.DataFrame, columns ...string) (*plot.Figure, error)
- func BarhOpts(df *dataframe.DataFrame, columns []string, opts []Option) (*plot.Figure, error)
- func Hist(df *dataframe.DataFrame, bins int, columns ...string) (*plot.Figure, error)
- func HistOpts(df *dataframe.DataFrame, bins int, columns []string, opts []Option) (*plot.Figure, error)
- func Line(df *dataframe.DataFrame, columns ...string) (*plot.Figure, error)
- func LineOpts(df *dataframe.DataFrame, columns []string, opts []Option) (*plot.Figure, error)
- func Scatter(df *dataframe.DataFrame, x, y string, opts ...Option) (*plot.Figure, error)
- func SeriesBar(s *dataframe.Series, opts ...Option) (*plot.Figure, error)
- func SeriesHist(s *dataframe.Series, bins int, opts ...Option) (*plot.Figure, error)
- func SeriesLine(s *dataframe.Series, opts ...Option) (*plot.Figure, error)
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
Area draws the columns as stacked filled areas against the index, as df.plot.area() does; a missing value counts as zero.
func Bar ¶
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 Hist ¶
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 ¶
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 Scatter ¶
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 SeriesHist ¶
SeriesHist draws the histogram of a series.
Types ¶
type Option ¶
type Option func(*config)
An Option adjusts a plot.
func WithLegend ¶
WithLegend turns the legend on or off. A frame plot has one by default, a series plot has none.
func WithXLabel ¶
WithXLabel sets the label of the x axis; by default it is the name of the index.