---
title: Plots
description: Draw charts from vectors, matrices and tables
---
```bpl
]help •plot
```

# `•plot` — Plots

`X •plot Y` returns a plot spec: a keyed vector holding the data `Y` and the settings `X`. Its [renderer](xml.md#rich-display) displays the spec as an SVG chart. Plain text on the left is shorthand for the `mark` setting. [Plots](plot.md) shows each mark and setting with examples.

The series and axes depend on the structure of `Y`:

- A vector's values are plotted against `0…n-1`.
- The keys of a keyed vector of numbers are the x labels.
- Each row of a matrix is plotted as a series. Row keys name the series. Column keys label x.
- With the `cell` mark, each row of a matrix is a row of cells, coloured by value. Row keys label the rows, and row 0 is at the top.
- Each column of a table, a keyed vector of equal-length columns, is plotted as a series. A column named `x` supplies the x values.
- A vector or matrix of plots is drawn as a figure.

| Setting | Holds | Default |
|--- |--- |--- |
| `mark` | `"line"`, `"point"`, `"bar"` or `"cell"` | `"line"` |
| `title` | Chart title | none |
| `width`, `height` | Size in pixels | `600`, `400` |
| `x`, `y` | `title`, `scale` (`"linear"` or `"log"`), `ticks`, and `axis` (`$f` hides that axis) |  |
| `legend` | `position` (`"end"` or a corner) and `border` | none |
| `grid` | `$f` hides the grid lines | `$t`, or `$f` for cells |
| `axes` | `$f` hides both axes, with their ticks and titles. An `axis` setting in `x` or `y` overrides it for that axis | `$t` |
| `flip` | `$t` swaps the axes | `$f` |
| `palette` | Colours for numbers: `"viridis"`, `"gray"`, or a list of colours | `"viridis"` |
| `colorbar` | `$t` shows the colour scale beside the plot | `$f` |
| `color`, `size`, `labels` | Styles for every series |  |
| `series` | Styles for one series, keyed by its name |  |
| `widths`, `heights`, `share` | Figure cell sizes and shared axis ranges |  |

`color` also takes one number per point. `palette` turns these numbers into colours, over the range of every series in the plot. A cell is coloured by its value unless `color` is set.

A direct [`•mime`](system/mime.md) call on a spec reports these errors: `DOMAIN` for unknown settings or values, and for cells mixed with other marks; `LENGTH` when series, colours, sizes or labels don't match the x values; `RANK` for data that isn't a vector, matrix or table.

<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Data

`•plot` draws one series for each row of a matrix. Its column keys label the x axis. With `"legend":"end"`, each line is labelled with its name beside its last point:

```bpl
sales←["city":"NY" "LA";"month":"Jan" "Feb" "Mar"]:[10 20 30◇40 50 60]
["legend":"end"] •plot sales
```

For a keyed vector of numbers, `•plot` uses the keys as x labels:

```bpl
•plot ["Jan":10 "Feb":20 "Mar":15]
```

In a table, a column named `x` supplies the x values. Every other column is a series:

```bpl
["mark":"point"] •plot ["x":150 160 170 180;"weight":50 62 68 80]
```

## Settings

`•plot` returns the plot spec, an ordinary keyed vector. Pass settings as the left argument of `•plot`, or assign them to the spec afterwards. Assigning through a dot path creates any missing settings along that path. Setting `legend.position` to a corner draws a boxed legend, titled with the name of the row axis:

```bpl
p←["mark":"bar" "title":"Sales"] •plot sales
p.legend.position←"top-left"
p
```

A `"log"` scale suits values that grow by factors:

```bpl
q←•plot 1 10 100 1000
q.y.scale←"log"
q
```

`ticks` is a number of ticks, a vector of positions, or a keyed vector from labels to positions:

```bpl
r←•plot 3 1 4 1 5
r.x.ticks←["first":0 "last":4]
r.y.ticks←3
r
```

## Series styles

`mark`, `color`, `size` and `labels` style every series. To style one series, put these fields under its name in `series`.

- `color` is `"#rrggbb"` or a colour name: `"blue"`, `"orange"`, `"green"`, `"red"`, `"purple"`, `"brown"`, `"pink"`, `"gray"`, `"olive"`, `"cyan"`, `"black"` or `"white"`. Series without a `color` are given the colours from `"blue"` to `"cyan"` in order. Give `color` one colour or one number per point to colour each point separately, as [Colour from data](#colour-from-data) describes.
- `size` is the point radius or line width in pixels. For points, pass one number per point to `size` to set each point's size by value.
- `labels` is `1` to label each point with its value, or one label per point. Use `""` to leave a point unlabelled. Overlapping labels are moved apart.

`•plot` does no curve fitting. Here the trend line is a least-squares fit computed in BPL and drawn as a second series:

```bpl
x←1 2 3 4 5 6 ◇ y←2.1 3.9 6.2 7.8 10.1 12.2
m←x*⊗0 1
fitted←["mark":"point"] •plot ["x":x "sales":y "trend":m+.×y⌹m]
fitted.series.trend.mark←"line"
fitted.series.sales.labels←1
fitted.legend.position←"bottom-right"
fitted
```

## Colour from data

Pass one number per point in `color` to colour each point by value. `palette` maps the numbers onto colours, from the lowest value in the plot to the highest. `palette` is `"viridis"` (the default), `"gray"`, or a list of colours spaced evenly along that range. With `"colorbar":$t`, the scale is shown beside the plot:

```bpl
["mark":"point";"size":8;"color":30 25 40 35;"colorbar":$t] •plot ["x":150 160 170 180;"weight":50 62 68 80]
```

The `cell` mark draws each row of a matrix as a row of cells, coloured by value. Row 0 is at the top, matching the printed matrix. Row keys label the rows:

```bpl
["mark":"cell" "colorbar":$t] •plot sales
```

A Sierpinski carpet is a 3×3 block with its centre removed. Each filled cell holds a smaller copy of the whole carpet. `kron` is the Kronecker product: each item of [`α`](https://bpl.fast.ai//glyphs/alpha.html) scales a copy of [`ω`](https://bpl.fast.ai//glyphs/omega.html). `block↣kron⍣3 block` applies `kron` three times, giving an 81×81 carpet. Setting `"axes":$f` leaves only the cells, without axes or ticks. `p.y.axis←$f` hides only the y axis. An `axis` setting in `x` or `y` takes precedence over `axes` for that axis. Because `•plot` draws each run of same-coloured cells in a row as one rectangle, the picture has far fewer SVG elements than cells:

```bpl
block←[1 1 1
       1 0 1
       1 1 1]
kron←{α×⍥ρω ρ 0 2 1 3⍉α×⊗ω}
plotopts←["mark":"cell";"palette":"white" "black";"axes":$f;"width":400;"height":400]
plotopts •plot block↣kron⍣3 block
```

## Figures

Pass a vector or matrix of plots to `•plot` to display them as a figure. A vector is one row, and [`⍪`](https://bpl.fast.ai//glyphs/comma-bar.html) makes a column. Repeat a plot in adjacent cells to make it span them. `widths` and `heights` give relative cell sizes. With `"share":1`, plots in the same column share one x range, and plots in the same row share one y range:

```bpl
["title":"Overview" "height":500] •plot [p p◇fitted •plot[3 1 4 1 5]]
```

The following example combines these figure settings to make a scatter plot with marginal histograms. Evaluating a normal distribution's `quantile` at evenly spread probabilities gives normally distributed values without randomness:

```bpl
n←•distribution "normal"
x←n.quantile 1|0.5+0.618034×ι400
y←0.6×x + 0.8×n.quantile 1|0.5+0.754878×ι400
```

`•plot` does no binning. `count` counts the values within 0.25 of each bin centre. Each histogram is a bar chart of counts. `flip` turns the second one on its side:

```bpl
centers←¯2.75+0.5×ι12
count←{+/ ω≥α-0.25 ∧ ω<α+0.25}
hist←{["x":centers "count":centers⁝count¨⊂ω]}
top←["mark":"bar"] •plot hist x
side←["mark":"bar" "flip":1] •plot hist y
```

With `heights` of `1 4`, the top histogram is a fifth of the figure's height. With `widths` of `4 1`, the side histogram is a fifth of the figure's width. The top-right corner is empty because its position holds [`⍬`](https://bpl.fast.ai//glyphs/zilde.html). With `share`, the top histogram shares the scatter plot's x range, and the side histogram shares its y range:

```bpl
scatter←["mark":"point" "size":2] •plot ["x":x "y":y]
grid←["widths":4 1;"heights":1 4;"share":1;"width":500;"height":500]
grid •plot [top ⍬◇scatter side]
```
