Metadata-Version: 2.4
Name: plottracer
Version: 0.2.0
Summary: Recover data from images of plots.
Keywords: digitize,plot,figure,image,data extraction
Author: Louie Slocombe
Author-email: Louie Slocombe <louies@hotmail.co.uk>
License-Expression: LGPL-3.0-or-later
License-File: COPYING
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Image Processing
Classifier: Typing :: Typed
Requires-Dist: numpy>=2.3.3
Requires-Dist: opencv-python>=4.10.0.84
Requires-Dist: onnxruntime>=1.24.1 ; extra == 'ocr'
Requires-Dist: rapidocr>=3.9.0,<3.10 ; extra == 'ocr'
Requires-Dist: pypdfium2>=5.0.0 ; extra == 'pdf'
Requires-Python: >=3.14
Project-URL: Homepage, https://github.com/SMTG-Bham/plottracer
Project-URL: Issues, https://github.com/SMTG-Bham/plottracer/issues
Project-URL: Repository, https://github.com/SMTG-Bham/plottracer
Provides-Extra: ocr
Provides-Extra: pdf
Description-Content-Type: text/markdown

# plottracer

Recover data from images of plots. plottracer finds the curves in a picture of
a plot, such as a scanned figure or a screenshot, and writes their points as
CSV in the plot's own units. Curves are told apart by colour. It reads plots in
PDF and SVG files too, exactly, from what they draw rather than from pixels.

It suits batches of similar plots: calibrate once, then digitize every image
the same way from the command line.

## Citing

Use **Cite this repository** on GitHub to copy the citation or download it
as BibTeX. Include the version you used. For an unreleased checkout,
include its Git commit as well. See
[CITATION.cff](https://github.com/SMTG-Bham/plottracer/blob/main/CITATION.cff).

## Installation

plottracer runs on Linux only, and needs Python 3.14 (a standard build;
OpenCV does not support the free-threaded build yet). Install the command
from [PyPI](https://pypi.org/project/plottracer/) with
[uv](https://docs.astral.sh/uv/):

```bash
uv tool install plottracer
```

or with pip:

```bash
python -m pip install plottracer
```

To install the latest commit rather than a release, install from GitHub, as
in `uv tool install git+https://github.com/SMTG-Bham/plottracer`. Version
0.1.0 was released on GitHub only, under the old name, figrecover, so
`git+https://github.com/SMTG-Bham/plottracer@v0.1.0` installs the command and
the Python package as `figrecover`.

To have plottracer read the axes for you (see
[Reading the axes](#reading-the-axes)), install the `ocr` extra instead. It
adds the [RapidOCR](https://github.com/RapidAI/RapidOCR) text reader and its
models, so it needs no network access when it runs:

```bash
uv tool install "plottracer[ocr]"
```

To read plots in PDF files (see [PDF files](#pdf-files)), install the `pdf`
extra, which adds the [pypdfium2](https://github.com/pypdfium2-team/pypdfium2)
PDF reader. Extras combine, as in `plottracer[ocr,pdf]`:

```bash
uv tool install "plottracer[pdf]"
```

SVG files (see [SVG files](#svg-files)) need no extra: plottracer reads them
with Python's own XML parser.

## Usage

### 1. Prepare the image

Keep the axes, tick labels, titles and legend in the image. plottracer traces
the area above the x-axis and to the right of the y-axis. A frame around the
axes bounds the trace on the other two sides. By default it traces the most
common curve colour, skipping filled areas and gridlines. It masks framed
insets and legends, and recognises unframed legends by their aligned samples
and labels. Keeping the legend also lets you select series by their
[markers](#markers). Dark backgrounds work as well as light ones.

A scan turned by a few degrees is levelled before tracing. A figure with
multiple panels or an inset can be read in one piece when plottracer reads
the axes. See [Turned images](#turned-images) and [Several panels](#several-panels).

With the OCR extra, plottracer excludes text boxes inside the axes and
looks beside them for straight shafts with an arrowhead or bracket cap.
It reports how many it found. A curve crossing an excluded box has a gap.
Use `--no-annotations` to keep these objects in the raster.

To exclude an annotation or stamp, give its bounding rectangle with
`--mask X0,Y0,X1,Y1`. Measure from the bottom-left corner of the original
image in pixels or of the PDF page in points. Repeat the option to mask
several regions. Samples inside the rectangles are omitted from the CSV.

### 2. Choose reference points

plottracer maps pixels to data with at least three reference points: usually
the origin, a labelled tick on the x-axis and one on the y-axis. Give their
data coordinates with `-p` and their pixel positions with `-l`, in the same
order. Pixel positions are measured from the bottom-left corner of the image,
so the centre of the bottom-left pixel is at 0.5,0.5.
With the `ocr` extra, you can leave out both and let plottracer read the tick
labels instead; see [Reading the axes](#reading-the-axes). For logarithmic
axes, see [Logarithmic axes](#logarithmic-axes).

plottracer traces the part of the image above and right of the lowest and
leftmost reference points, so take them on the axis lines. If they lie a
little below or left of an axis line, up to 12 pixels, the part traced still
starts just beyond the line, as if they had been taken on it, though the
points are calibrated as given. A reversed axis,
whose values fall from left to right or from bottom to top, starts at its
largest value, so take the corner where the axes cross rather than the
origin, and ticks on the axis lines.

If you leave out `-l`, plottracer opens the image and asks you to click on each
reference point in turn (press q to cancel). It then prints the matching `-l`
options, ready to reuse for the rest of a batch of similar plots:

```text
plottracer: locations: -l 80.5,43.5 -l 576.5,43.5 -l 80.5,147.5
```

### 3. Digitize

```bash
plottracer examples/spectrum.png \
    -p 0,0 -p 10,0 -p 0,1 \
    -l 80,44 -l 576,44 -l 80,147 \
    -o spectrum.csv --preview spectrum.preview.png
```

The CSV file has a header and a row for each image column the curve crosses,
or more than one where it crosses a column more than once, as a loop does:

```text
x,y
0.0705645,0.495146
0.0907258,0.495146
...
```

Without `-o`, the CSV goes to standard output. The preview draws the recovered
curve in its own colour over a faded copy of the plot, and the reference points
in blue, so you can check the result at a glance:

![The input plot](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/spectrum.png)
![The recovered curve drawn over the plot](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/spectrum.preview.png)

| Option | Meaning |
| --- | --- |
| `-p`, `--point X,Y` | Data coordinates of a reference point. Repeat for at least three. Default: [read the axes](#reading-the-axes). |
| `-l`, `--location X,Y` | Pixel position of each reference point, in the same order. Default: click on them. |
| `--log AXES` | The axes of the `-p` reference points that are logarithmic: `x`, `y` or `xy`. Default: neither. |
| `-n`, `--series N` | Trace N curves, those of the most common colours first, or in a PDF file the longest, and print the matching `-c` options. Default: 1. |
| `-c`, `--colour RRGGBB[:STYLE]` | Hexadecimal colour of a curve to trace, such as `1f77b4`, with a style to pick one of several curves of that colour, such as `000000:dashed`. Repeat for several curves. See [Several curves](#several-curves). |
| `-m`, `--marker X,Y` | Pixel position of the marker in a series' legend key, or in a PDF file its position in points, to find the series by its markers' shape and colour. Repeat for each series. See [Markers](#markers). |
| `-o`, `--output FILE` | CSV file to write. Default: standard output. |
| `--classes IMAGE` | Save a colour-coded image of the detected plot objects. For several inputs, give a directory. |
| `--preview PATH` | Save a preview image. For several inputs, give a directory for the previews. |
| `--metadata FILE` | Save a JSON record of the digitization. |
| `--axes-from FILE` | Reuse the recorded ticks and axis titles for a matching single plot. |
| `--no-annotations` | Keep text and arrows when tracing image curves. |
| `--mask X0,Y0,X1,Y1` | Exclude a rectangle before tracing. Repeat for multiple regions. |
| `--remove-grid` | Paint over grid lines before tracing. |
| `--level DEGREES` | The angle by which the image's axes are turned anticlockwise, or clockwise if negative, to turn it back by. Default: find it; see [Turned images](#turned-images). |
| `--no-level` | Read the image as it is, without levelling it. |
| `--trace-range` | Append `trace_low` and `trace_high`, the measured vertical edges of the raster stroke in data units. |
| `--error-bars` | Also read the error bars of each marker, and add their bounds to the CSV. See [Error bars](#error-bars). |
| `--page N` | The page of a PDF file to read, from 1. Default: 1. |
| `--figure LABEL` | Select a captioned PDF figure by its label, such as `2` or `S1`. |
| `--box X0,Y0,X1,Y1` | The part of a PDF file's page to read, such as one figure, as its left, bottom, right and top edges in points. Default: the whole page. |

### Batches

Give several files to write one table with a leading `source` column.
For example, to recover four curves from each of a set of similar plots:

```bash
plottracer plots/*.png -n 4 -o curves.csv --preview previews/
```

With `-n`, the first successful input supplies the colours and styles for
each panel. Later files reuse those selectors so the series numbers stay in
the same order. Use a separate run for plots with different series.
With `-p`, click once and use those positions throughout the batch.
Without `-p`, plottracer reads the axes separately for each input.

Preview files are named `<stem>.preview.png` in the directory you give.
Repeated stems receive a number, such as `plot.2.preview.png`. With `-o`
and named panels, each panel gets a CSV containing its sources. A failed
input is reported by name. The remaining inputs still run and the command
returns status 1 if any failed.

### Measuring stroke extent

```bash
plottracer examples/labelled.png --trace-range -o voltage.csv
```

At each observed curve point, `trace_low` and `trace_high` give the outer
edges of the lowest and highest pixels of the stroke in that column. A thin
horizontal line may occupy just one pixel. A steep peak can occupy dozens.
Both edges are mapped through the plot's calibration, including logarithmic
and reversed axes. The columns follow any experimental error-bar columns.
They are empty for marker selections, inserted points without a measured run,
and vector documents. The span describes visible ink. Calibration error and
parts of a curve hidden behind other marks can put the underlying data outside it.

### Recording a digitization

```bash
plottracer examples/labelled.png -o voltage.csv --metadata voltage.json
```

The JSON records the command arguments, UTC time, software versions and the
SHA-256 of each input. Each plot records its reference points or ticks, axis
titles and scales, largest fit residual in pixels (points for vector drawings),
and the number of CSV rows for each series. Automatically discovered curves
also record their measured colour, style and stroke width. Marker keys and
clicked locations use the original input coordinates. Each input retains its
warnings and any error, including when another input in the batch succeeds.

For a single plot, run the command in its `reproduce` field from the original
working directory. It writes the CSV to standard output. Keep the JSON with
the input: `--axes-from` checks the input hash and restores the measured ticks
and titles without OCR. For hand calibration the command includes the recorded
locations. Figures with multiple panels or insets record each plot separately.

### Several curves

When a legend is read, CSV series use its printed names and follow its
order. Explicit `-c` and `-m` selections keep the supplied order. Each
unmatched series keeps its number. Legend rows inside the axes are excluded
from tracing, including unframed samples. With manual calibration and no
OCR, geometric samples can still be masked and the series keep numbers.

Give the number of curves with `-n`:

```bash
plottracer examples/series.png \
    -p 0,0 -p 10,0 -p 0,1 -l 80,44 -l 576,44 -l 80,147 \
    -n 4 --preview series.preview.png
```

plottracer traces the four most common colours that stand out from the
background, the curve with the most pixels first, and prints their colours as
`-c` options:

```text
plottracer: colours: -c 2ca02c -c 1f77b4 -c ff7f0e -c 000000
```

The CSV gains a `series` column that numbers the curves in that order:

```text
series,x,y
1,0.0705645,1.2767
1,0.0907258,1.28641
...
```

![Four crossing curves, each recovered in its own colour](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/series.preview.png)

Which curve has the most pixels can change from one plot to the next.
Across separate runs, pass the printed `-c` options to fix the order of
the series, and each picks the curve whose colour is nearest, so they also
work on similar plots whose colours differ slightly, such as JPEG files. Give
only the `-c` options of the curves you want. The colours of plotting
libraries work too, such as `-c 1f77b4` for matplotlib's first colour.

Lines thinner than a pixel never show their full colour, as antialiasing
blends them with the background, nor do lines drawn translucent: a
`tab:blue` line at half opacity shows as `#8ebad9`. plottracer finds such
lines by the direction of their colour from the background, which their
blends share, and prints the strongest colour each shows, such as `eda2a3`
for a `tab:red` line a quarter of a point wide at 100 dots per inch. A `-c`
colour picks such a line too, as the line shows part of it: `-c d62728` picks
that line, and `-c 1f77b4` the translucent one.

For a thin line in a JPEG, compression may spread the hue across the stroke's
edges. When those fringes are widespread, plottracer uses nearby colour and
the pixel's brightness to distinguish strokes. The printed colour comes
from the original pixels. A nominal colour can still select the series.

A stroke passing through a shaded band
can change colour again. When the blended segment touches the exposed
stroke and its colour fits the two layers, plottracer traces them as one
curve. The band itself is excluded from the CSV.

`-n` skips colours that fill areas, those drawn mostly as straight lines
across the plot, such as grid lines, dashed or not and even with curves drawn
over them, broad bands around a thinner stroke, overlaps of translucent
colours, and very pale colours, less than 64 levels from the background; trace a
pale curve with `-c` instead. Curves in neighbouring colours of a colour map,
such as viridis, are told apart, and two `-c` colours that match the same
curve are an error. When `-n` finds too few curves, it gives the `-c` options
for those it found and explains which colours it left out. For example,
asking for three curves in [the grid example](#grids) gives this error.

```text
plottracer: error: found only 1 of 3 curves, -c 000000; left out #404040 (3078 pixels), as it is drawn in lines across the plot, as grid lines are
```

In a greyscale image, such as a photocopy, colours that differed may have
become one grey, and the edges of a dark curve, blended with the background,
show the lighter greys of other curves. plottracer leaves those edges out of
the lighter curves, and with several curves asked for, says that the image
is greyscale, as curves of different colours may then share a grey. Such
curves are told apart as other curves of one colour are, below: in a
greyscale copy of `examples/series.png`, the blue and green curves both
become `#5c5c5c`, and `-n 4` gives them as `-c 5c5c5c:solid` and
`-c 5c5c5c:solid2`. Series can be found by their [markers](#markers) too.

For a greyscale scan with a regular dot screen, plottracer measures the
screen's spacing and angle and averages over one repeating cell before
finding the curves. The axes are read from the original scan.

Curves that share a colour are told apart by how they are drawn and where
they run. plottracer follows each curve of a colour across the image,
through the places where it crosses or touches another, and across the gaps
between its dashes, and names its style from its dashes: `solid`, `dashed`,
`dotted` or `dashdot`. `-n` lists each as a curve of its own, and prints the
`-c` option that picks it, the colour followed by the style:

```bash
plottracer examples/styles.png \
    -p 0,0 -p 10,0 -p 0,1 -l 80,44 -l 576,44 -l 80,147 \
    -n 3 --preview styles.preview.png
```

```text
plottracer: the image is greyscale, so curves of different colours may share a grey
plottracer: colours: -c 000000:solid -c 000000:dashed -c 000000:dotted
```

![Three crossing black curves, solid, dashed and dotted, each recovered in a colour of its own](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/styles.preview.png)

Colours of one curve come in their usual order, and the curves of a colour of
several come in among them by their pixels, most first: so the mean of pale
replicates, drawn in the full colour, comes before the replicates, which cross
one another and break into many pieces. The preview draws curves that share a
colour in colours of their own. A dashed or dotted curve gives a point in
every column, those between its dashes on the straight line across the gap. On
a plot of black solid, dashed and dotted curves at 1.5 points and 100 dots per
inch, each curve lies within 0.005 of the y-axis range of the true curve in
98% of the columns, and two black lines that cross, as an X, keep to their own
lines.

In a PDF file, the style is read exactly from the dashes that each line is
drawn with, a dash at most two line widths long being a dot, and lines of
other widths, or drawn apart, are curves of their own:

```bash
plottracer examples/styles.pdf -n 3
```

```text
plottracer: colours: -c 000000:dotted -c 000000:dashed -c 000000:solid
```

A second curve of the same colour and style is numbered, such as
`-c 000000:solid2`, and the colour alone, `-c 000000`, still takes every curve
of that colour, as one, and warns if that one crosses most columns more than
once, as several solid curves do, or a loop.

A colour drawn with filled markers and a line, such as data points and a curve
fitted to them, gives the markers first, as a series of their own in the style
`markers`, with a point at the middle of each marker, and then the line. On a
plot of 19 points scattered about a fitted line, all in one colour, `-n 2`
gives the 19 points, each within a pixel, and the line, within 0.005 of the
y-axis range in 99% of the columns. In a PDF file, likewise, a line in the
colour of markers is a curve of its own unless it passes through the markers
alone, as a line joining them does.

### Markers

For a shared marker legend, give each key position in the original figure.
The same samples are used to find markers in each panel, including when the
legend sits above the panels or inside only one panel.

When the series are told apart by their markers, as in many spreadsheet
charts, give the pixel position of the marker in each series' legend key with
`-m`, measured from the bottom-left corner like `-l`:

```bash
plottracer examples/markers.png \
    -p 0,0 -p 10,0 -p 0,1 -l 55,83 -l 627,83 -l 55,185 \
    -m 148,18 -m 260,18 -m 377,18 -m 483,18 --preview markers.preview.png
```

plottracer cuts each key's marker out of the legend, in its colour, leaving
out the line through it, and finds the markers of that shape and colour in
the plot. The CSV has a point at the middle of each marker, numbered by series
in the order of the `-m` options, and plottracer prints how many markers each
series has:

```text
plottracer: markers found in each series: 19, 19, 19, 19
```

The preview rings each marker, and each key, in a colour for its series:

![Four series told apart by their markers](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/markers.preview.png)

- The markers can be filled or open, such as squares, diamonds, triangles,
  circles and crosses. Each series needs a shape or a colour of its own:
  series of one shape in several colours, as in `examples/coloured.png`,
  are told apart, and so are series that mix shapes and colours.
- Shapes that look alike at the image's size are told apart by fitting
  each to the marker to a fraction of a pixel. In `examples/similar.png`,
  whose circles, pentagons, hexagons and octagons are 6 points, or 8
  pixels, across, every marker is found in its series, and plottracer warns
  of the circles' keys, alike with the hexagons' and the octagons':

  ```text
  plottracer: the markers of the keys at 98,339 and 270,339 look alike at this size, so some may be taken for each other
  plottracer: the markers of the keys at 98,339 and 355,339 look alike at this size, so some may be taken for each other
  ```

  A marker that another key's marker fits almost as well is ambiguous, and
  plottracer prints how many each series has, and where, in the plot's x
  units, such as `series 1: 19 found, 2 of them ambiguous near x = 3.5, 7`.
- Lines and error bars that cross a marker are allowed for. A marker partly
  covered by another, as where series cross or crowd together, is found by
  what shows of it. Where the other series have markers at an x and a
  series has none, under another's marker, its marker is probably hidden
  there, and plottracer says so. In `examples/overlapping.png`, diamonds
  cover the tops of the circles below them, and the four series cross at
  x = 5:

  ```bash
  plottracer examples/overlapping.png -p 0,0 -p 10,0 -p 0,1 \
      -l 80,44 -l 576,44 -l 80,147 -m 98,339 -m 184,339 -m 270,339 -m 355,339
  ```

  ```text
  plottracer: markers found in each series: 17, 20, 19, 18
  plottracer: series 1: 17 found, 2 probably hidden near x = 4, 5
  plottracer: series 4: 18 found, 1 probably hidden near x = 5
  ```

  The circle at x = 5 lies wholly under a square. At x = 5.5, the
  exposed third of the circle is enough to identify it. Markers mostly hidden
  can also be taken for another's: there, one square is a triangle, one
  triangle a diamond and one diamond a circle. Check the preview.
- A series without markers gets a warning, as its `-m` position or the axes
  may be wrong.
- Compressed images work too: `examples/markers.png` saved as a JPEG of
  quality 25 gives the same markers.
- The legend must be in the image, but may be in the plot area: its keys are
  not taken for markers. Nor are markers in an inset with a frame.
- In a PDF file, markers are read exactly, from what the PDF draws; see
  [PDF files](#pdf-files).

### Error bars

With `--error-bars`, plottracer also reads the error bars drawn through
each marker, and writes their ends in the plot's units:

```bash
plottracer examples/errorbars.png -n 4 --error-bars \
    -o errorbars.csv --preview errorbars.preview.png
```

Each colour drawn with markers and a line gives its markers first, as a
series of their own (see [Several curves](#several-curves)), so `-n 4`
gives the four series of markers, and `-n 7` adds the three lines:

```text
plottracer: colours: -c 2ca02c -c ff7f0e:markers -c 1f77b4:markers -c d62728:markers
```

The CSV gains `y_low` and `y_high` columns, the lower and upper ends of
each marker's vertical bar, and, if any marker has a horizontal bar,
`x_low` and `x_high` columns, its left and right ends. Each marker has a
row at its middle. The rows of a line traced between markers leave the
bounds empty, as does a marker for a bar it does not have, or whose end
cannot be seen:

```text
series,Time (s),Voltage (mV),y_low,y_high,x_low,x_high
1,0.521097,1.54329,1.40044,1.69914,0.218809,0.823385
1,1.50857,1.6342,1.47836,1.79004,1.22644,1.81086
...
2,0.521097,2.62121,2.51731,2.91991,,
...
```

The preview draws each bar, with a short line across each end, in its
series' colour:

![Four series of markers with error bars, each bar recovered in its series' colour](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/errorbars.preview.png)

- A marker is a filled shape of its series' colour, a pixel or more
  thicker each side than the series' lines, and its point is the middle of
  its box. Its bars run up, down and across from its edges, in its colour,
  to their ends, or to the middles of their caps. Open markers, and lines
  drawn without markers, give no bounds; give `-m` for open markers.
- Bars are left out of the curves traced, with or without `--error-bars`,
  so bars longer on one side, or across, no longer pull a curve away from
  its points. On `examples/errorbars.png`, the orange line, whose bars run
  further above than below, strays up to 0.007 of the y range from the
  line through its points, where it strayed 0.025 with them, and the green
  markers, with bars across, give points only within their markers.
- A bar may cross a line of another colour, but an end hidden by another
  colour, by its marker or beyond the axes is left empty. A bar without a
  cap must run 5 pixels beyond its marker, and be straight, so that a line
  through a marker is not taken for one. A horizontal bar along a line of
  its colour that runs level through its markers cannot be told from it,
  and is left empty, as is a bar in another colour than its marker.
- With `-m`, the bars of each series' markers are followed in ink of any
  colour, as such series often share one.
- Bounds are the ends of the bars as drawn, not distances from the point.
  On `examples/errorbars.png` every end lies within 2 pixels of the truth,
  and the markers' points within a pixel, but the triangles' points within
  2.5, as their boxes' middles lie lower than their points.
- In a PDF file, error bars are read exactly; see [PDF files](#pdf-files).

### Grids

Grids need no special handling, light or dark, because grid lines run
straight across the plot and curves do not:

```bash
plottracer examples/grid.png \
    -p 0,0 -p 10,0 -p 0,1 -l 80,44 -l 576,44 -l 80,167 \
    --preview grid.preview.png
```

![A damped oscillation recovered from a plot with a dark grid](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/grid.preview.png)

A grid drawn in the same colour as a curve has to be removed first, with
`--remove-grid`.

### Reading the axes

With the `ocr` extra installed, leave out `-p` and `-l`, and plottracer reads
the tick labels and axis titles itself:

```bash
plottracer examples/labelled.png --preview labelled.preview.png
```

It prints what it read, so you can check it:

```text
plottracer: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "Voltage (mV)" with ticks at -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1
```

and names the CSV columns after the axis titles:

```text
Time (s),Voltage (mV)
0.00688239,0.999744
...
```

The preview circles each tick it read, on its axis line:

![A damped oscillation recovered from a plot whose axes were read](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/labelled.preview.png)

Each image is calibrated from its own labels, so the plots in a batch can have
different axes. The other options, such as `-n`, `-c` and `--remove-grid`,
work as before.

- The x-axis tick labels are the lowest row of three or more numbers, and the
  y-axis tick labels the leftmost column of three or more numbers above them,
  though the lowest may reach down beside them, as rotated labels can.
  Labels whose values are out of line with the rest, such as misread ones, are
  dropped. On a linear axis, labels must also take the even steps that
  plotting libraries label at: at low resolution OCR misreads digits as
  others, such as 750 as 758, close enough to their ticks to look in line,
  and those are dropped too. An axis with fewer than three labels in line
  cannot be read, so neither can a linear axis labelled at uneven values,
  such as 0, 273 and 373; give `-p` instead.
  Logarithmic axes are read too; see [Logarithmic axes](#logarithmic-axes).
  So are reversed axes, whose values fall from left to right or from bottom
  to top, as depth often does.
- Numbers can have thousands separators (`2,000` or `2 000`), a percent sign
  (`50%`, read in percent) or an exponent (`2.5e-3`). A tick mark that OCR
  reads as a dash beside a label, as it can where no axis line joins them, is
  ignored.
- Decimal commas work too, with points or spaces as thousands separators
  (`0,5` and `2.000`), as many European plots write them. A plot's labels
  decide: a label such as `0,5` reads as a number only with decimal commas,
  `0.5` only with decimal points, and `2.000` either way, so plottracer takes
  the way more labels read only one way, decimal points if neither wins. A
  single label misread with a comma does not outvote the rest.
- OCR can mistake letters for digits, and dashes for minus signs. In a label
  with a digit, O and o are read as 0, and l, I and | as 1 before a digit or
  a point, so `1O` and `l.5` read as 10 and 1.5; after a digit, they may be a
  unit, such as `5 l`, and are not read. Any dash before a number, such as
  an en or em dash, is a minus sign.
- A scale factor printed where plotting libraries put one, such as `1e-5` or
  `×10³` above the y-axis, or below the right half of the x-axis, multiplies
  that axis's tick values. Above the y-axis, it may lie beside the top half
  of the topmost tick label, as matplotlib prints it when that tick is at the
  top of the axis. Text elsewhere that looks like one, such as a legend entry,
  or the scale factor above the y-axis of a plot below, does not. An axis
  offset, such as `+1e3`, cannot be read.
- A power of ten in an axis title, after a bracket or a slash, as in
  `Current (×10⁻³ A)`, `Count (×10³)` or `Energy / 10³ J`, says the axis is in
  units of that power, so it multiplies the tick values too, and is left out
  of the column's name: `Current (A)`, `Count` or `Energy / J`. Units are not
  converted, so a column titled `Voltage (mV)` stays in millivolts.
- Each tick lies at the middle of its label, or at a tick mark: for labels
  that end at their tick marks rather than centring on them, as rotated
  x-axis labels do, at the mark at that end; for labels that lie alike to
  one side of their marks, as matplotlib draws rotated y-axis labels, at the
  mark beside each; and for rotated labels centred on their marks, at the
  mark, as OCR's boxes put the middles of rotated text up to 2.4 pixels off.
  Labels written across stay at their middles, which OCR finds to about a
  pixel: matplotlib draws its marks on whole pixels, on average half a pixel
  from where it draws the curves, so they are no surer.
- Tick labels can be rotated to any angle, on either axis. plottracer finds
  how far the labels slant from their ink, from the long axis of each
  character, and if OCR misread a slanted label, missed one, or lost
  characters of one, it reads the labels again with the image turned to lay
  them level, which takes one more OCR pass, about half a second. Labels
  written upwards or downwards are read again turned a quarter turn each
  way, two passes, about a second, and the turn that puts more of them in
  line wins, or that OCR reads more surely, as it reads them less surely
  upside down. On the y-axis, OCR runs such labels together, as in
  `1.25 1.50 1.75` or `-0.25-0.50`, and misreads some, such as `2.5` as `25`
  or a 6 as 9, so they are read again whenever most are taller than wide,
  or OCR did not read them well. On test plots with labels at 30°, 45°, 60°
  and 90° either way, on axes from 0 to 1, 10 or 1000, -1 to 1 and of years,
  every tick was read, but for one at the end of an axis that OCR missed,
  and placed at its mark, and the curves lay within 0.0023 of the y-axis's
  range by the median. Labels that OCR misreads even turned are dropped.
- OCR can lose characters of a label, such as its decimal point, reading
  0.5, 1.0 and 1.5 as `5`, `10` and `15`: values in line, but ten times too
  large. So a label read as fewer characters than there are pieces of ink
  wholly inside its box is dropped, and an axis left with fewer than three
  labels is not read: plottracer stops with `OCR lost characters, such as
  decimal points, of 3 of the 5 y-axis tick labels`.
- plottracer traces the curves from the axis lines, not from the first ticks,
  so the margins that plotting libraries leave inside the axes are kept. If
  there are no axis lines, it starts just beyond the tick labels and their
  tick marks.
- A plot with a second y-axis on the right, with other values than the left
  one, cannot be read: crop the right axis out, or give `-p`.
- The titles are the nearest text below the x-axis tick labels and left of the
  y-axis ones, centred anywhere along their axes, so a title aligned with
  either end of its axis is read too. A title can run over several lines, as a
  `\n` breaks it, each within 1.25 line heights of the one before, by the
  taller of the two, and lined up with it, so a caption beside it, or further
  below, is left out; and the words of a line lie within a line height of
  each other, so text further along, such as the labels of other plots, is
  left out too. Tick labels broken over lines, such as `0\ns` with units
  under each number, are read by their first lines, and their other lines are
  not taken for the x-axis title. A y-axis title written upwards, as most are,
  is read again with the text left of the tick labels turned upright, which
  OCR reads more surely, and so is one written downwards, as titles of
  right-hand axes are, as OCR turns it the right way up; one written across
  is read as it is. A title drawn over the tick labels, as matplotlib draws
  one turned to 270° without more padding, cannot be read, nor can the
  labels it covers. A column without
  a title is named `x` or `y`. Titles of a single letter are easily misread,
  such as `x` as `X`, or missed.
- Titles keep their Greek letters, symbols, superscripts and subscripts, such
  as `ΔE (%)`, `Wavelength (μm)` and `CO₂ (mg L⁻¹)`, and the CSV header
  writes them in UTF-8. OCR reads some symbols as either of two that look
  alike, so each is written one way: the increment sign as Greek Δ, the micro
  sign as Greek μ, and full-width signs, such as `％`, as plain ones. It can
  also read the raised minus sign of a superscript full size, so a minus sign
  before a superscript digit is raised, and `10−³` reads as 10⁻³, in titles
  and tick labels alike. OCR reads symbols less surely than letters, so
  check the printed titles. Turned upright, it has read `ΔE` as `ΔΔE`, so if
  a title reads upright as it first did but with a character twice,
  plottracer takes whichever reading OCR is surer of; `ΔΔG`, read alike both
  ways, is kept.

The titles of `examples/titled.png` run over two lines:

```bash
plottracer examples/titled.png
```

```text
plottracer: x-axis "Photon energy (eV)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "Absorbance (arb. units)" with ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3
```

The x-axis tick labels of `examples/slanted.png` are written at 45°, which
OCR misreads as drawn, reading 2 as a Chinese character and 6 as 9, but reads
turned level:

```bash
plottracer examples/slanted.png
```

```text
plottracer: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "Voltage (mV)" with ticks at 0, 0.5, 1, 1.5, 2, 2.5
```

So are those of `examples/tilted.png`, at 30° on the x-axis and 45° on the
y-axis, and `examples/steep.png`, at 60° on the x-axis and upwards on the
y-axis. Those of `examples/downwards.png` are written downwards on both
axes, as is its y-axis title:

```bash
plottracer examples/downwards.png
```

```text
plottracer: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "Voltage (mV)" with ticks at 0, 0.5, 1, 1.5, 2, 2.5
```

When a plot's labels cannot be read, give `-p` as before.

### Logarithmic axes

When plottracer reads the axes, it finds logarithmic ones itself: an axis is
logarithmic if more of its tick labels fall in line on a logarithmic scale
than on a linear one. It says so as it prints the ticks:

```bash
plottracer examples/logarithmic.png --preview logarithmic.preview.png
```

```text
plottracer: logarithmic x-axis "Frequency (Hz)" with ticks at 1, 10, 100, 1000, 10000
plottracer: logarithmic y-axis "Power (mW)" with ticks at 0.1, 1, 10, 100, 1000
```

![A low-pass filter's response recovered from a plot with logarithmic axes](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/logarithmic.preview.png)

The tick labels can be decimals, such as `0.01` and `1000`, or powers with
superscript exponents, such as 10⁻², 2×10³ and 2⁵, as matplotlib draws them.
OCR often misses a superscript, reading 10² as `102` and 2⁵ as `25`, so
plottracer checks how a label of digits is drawn: if its last digit sits
raised above the others, the raised digits are an exponent and the full-size
ones its base, and a raised minus sign before the exponent makes it negative.
Each axis takes the base that most of its powers have. A label that reads as
that base followed by digits, but is not drawn that way, is still a power if
most of the others on its axis are, so an axis whose labels all read as
`100`, `101`, `102` is logarithmic when they are drawn as 10⁰, 10¹, 10²,
while labels really drawn as 100, 101, 102 stay linear, even if one is
misread as a power. As on a linear axis, three labelled ticks are needed, so
an axis labelled only at 10⁰ and 10¹ cannot be read.

The plot in `examples/binary.png` is labelled with powers of two, most of
whose superscripts OCR misses:

```bash
plottracer examples/binary.png
```

```text
plottracer: logarithmic x-axis "Size (KiB)" with ticks at 1, 2, 4, 8, 16
plottracer: logarithmic y-axis "Time (ms)" with ticks at 0.25, 0.5, 1, 2, 4, 8, 16, 32, 64
```

Superscripts are small, so at low resolution OCR can misread them as other
digits, such as 2¹⁶ as `226`, and an axis whose exponents are all misread
alike is read wrongly. Check the printed ticks.

With `-p`, name the logarithmic axes with `--log x`, `--log y` or `--log xy`.
Reference points on a logarithmic axis must be positive, so take the lowest
labelled tick as the origin, rather than zero:

```bash
plottracer examples/logarithmic.png \
    -p 1,0.1 -p 1e4,0.1 -p 1,1e3 -l 93,66 -l 610,66 -l 93,380 --log xy
```

### Several panels

A figure-level legend applies across the panels. A legend inside one panel
also names matching series in the others. The CLI reports the legend scope,
names and reusable selectors. In vector PDFs, distinct plot clips and the
printed legend allow the panels to retain separate calibrations.

A figure of several plots, such as panels labelled (a), (b), (c), is read
panel by panel when plottracer reads the axes:

```bash
plottracer examples/panels.png -o panels.csv --preview panels.preview.png
```

It prints what it read of each panel, named after its label, and the files
it wrote:

```text
plottracer: panel a: x-axis "Energy (eV)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: panel a: y-axis "Intensity" with ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3
plottracer: panel b: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: panel b: y-axis "Voltage (mV)" with ticks at -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1
plottracer: panel c: x-axis "Depth (m)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: panel c: y-axis "Temperature (K)" with ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3
plottracer: panel d: logarithmic x-axis "Frequency (Hz)" with ticks at 1, 10, 100, 1000, 10000
plottracer: panel d: logarithmic y-axis "Power (mW)" with ticks at 0.1, 1, 10, 100, 1000
plottracer: wrote panels_a.csv, panels_b.csv, panels_c.csv, panels_d.csv
```

Each panel's CSV is like a single plot's, with its columns named after the
panel's own axis titles. Without `-o`, standard output gets one CSV with a
`panel` column first, whose other columns are named after the axis titles if
every panel has the same ones, or `x` and `y`:

```text
panel,x,y
a,0.117437,0.505713
...
```

The preview draws every panel's curves and ticks on the one image:

![Four panels, each recovered with its own axes, one of them logarithmic](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/panels.preview.png)

The panels of `examples/shared.png` share their axes, so matplotlib labels
the x-axes only on the lower panels, and the y-axes only on the left ones.
Each panel takes the axes that it lacks from those beside it:

```bash
plottracer examples/shared.png -o shared.csv
```

```text
plottracer: panel a: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: panel a: y-axis "Voltage (mV)" with ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3
plottracer: panel b: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: panel b: y-axis "Voltage (mV)" with ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3
...
plottracer: wrote shared_a.csv, shared_b.csv, shared_c.csv, shared_d.csv
```

An inset, a smaller plot drawn in a frame inside a plot's axes, as
matplotlib's `inset_axes` draws one, is read as a panel of its own, with its
own axes, while the plot's curves are traced with it painted out:

```bash
plottracer examples/inset.png -o decay.csv --preview inset.preview.png
```

```text
plottracer: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "Voltage (mV)" with ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3
plottracer: panel inset: x-axis with ticks at 6, 7, 8, 9, 10
plottracer: panel inset: y-axis with ticks at 0.1, 0.2, 0.3
plottracer: wrote decay.csv, decay_inset.csv
```

The plot keeps the name it has without the inset: none, in an image of one
plot, so its curves go to `decay.csv`, as they would without the inset, and
the inset's, in its own units, to `decay_inset.csv`. Without `-o`, standard
output gets one CSV with a `panel` column, empty for the plot and `inset`
for its inset. An inset is named after the panel it lies in, such as
`b_inset` in panel (b), and several insets in one panel are numbered in
reading order, such as `inset1` and `inset2`.

![A plot and its inset, each recovered with its own axes](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/inset.preview.png)

- The panels are found by their axes: a horizontal and a vertical axis line,
  each at least a tenth of the image's shorter side long, that meet, as
  matplotlib's frames do too. An image with one such plot, or none, such as
  plots without axis lines, is read as one plot, as before. Axes inside
  another's are found as insets instead.
- An inset is a frame inside a panel's plot area, one of those painted out
  before the panel's curves are traced (see [How it works](#how-it-works)),
  whose axes can be read. They are read from its part of the plot area,
  which holds its tick labels and titles and runs up to just short of the
  other frames there, as a panel's part runs up to the panels around it.
  The text in it is that already read in the panel, so an inset takes
  little longer to read. Its curves are traced inside its frame. A frame
  whose axes cannot be read, such as a legend's, or an inset's without tick
  labels, stays painted out, without a warning.
- Each panel is read from its part of the image, which holds its tick labels
  and titles, and runs up to just short of the axes of the panels around it.
  On the right, it stops at the tick labels or title of the panel beside it,
  so they are not read as a second y-axis.
- Panels are named in reading order, in rows from the top, each from the
  left. A panel's label is the text nearest the top-left corner of its axes,
  inside or outside them, within a quarter of their width and height, written
  as a letter: (a), a), a. or a. OCR misreads single letters in brackets,
  such as (b) and (d) as (p), and misses some, but panels are labelled in
  reading order, so if more than half the labels read are a, b, c… in that
  order, counting a p read for b or d, every panel takes its letter in that
  order, as capitals if most of those read are. Otherwise each panel keeps
  the letter read, and one without, or with a letter read for another panel
  too, is named by its number in reading order. The labels of other panels
  are not read as a panel's titles, such as the label of the panel below, if
  it lies above that panel's axes.
- A panel label inside the axes is masked before tracing. If OCR misses it,
  its ink can appear in a curve of the same colour. Use `--mask` to exclude
  the label's rectangle.
- Plots that share an axis often label its ticks only on the outer panels:
  an x-axis on the lowest panel of a column, and a y-axis on the leftmost of
  a row. A panel without tick labels on an axis takes that axis, and its
  title, from the nearest panel read that lines up with it along the axis:
  in the same column for the x-axis, or the same row for the y-axis, with
  the ends of its axes within 5% of their length of the panel's. Its ticks
  lie where that panel's do, in the whole image. So a panel can take its
  x-axis from the panel below it and its y-axis from the panel beside it.
- A panel whose axes cannot be read, such as a colour bar with a frame, or a
  panel without tick labels on an axis that no panel read lines up with, is
  left out with a warning, as is one whose curves cannot be traced.
  plottracer fails only if no panel is left.
- `-n` and `-c` apply to each panel, and `-n` prints the colours of each.
  `-m` needs an image of one plot, and leaves its insets painted out, unread.
  With `-p`, the image is one plot, and its insets are painted out, as
  before.
- Each panel takes about as long to read as a plot, about two seconds.

### Turned images

A scanned or photocopied plot is seldom quite level, and a turn of a quarter
of a degree, two pixels across a plot, is enough for its axis lines or frame
to be traced as part of a curve. So plottracer finds the angle by which an
image's axes are turned, and turns the image back before reading it:

```bash
plottracer examples/skewed.png --preview skewed.preview.png
```

```text
plottracer: turned the image 1.00° clockwise to level its axes
plottracer: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "Voltage (mV)" with ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3
```

The preview is drawn on the image as given, with the curves and the ticks
turned back onto it:

![A damped oscillation recovered from a plot turned by 1°](https://raw.githubusercontent.com/SMTG-Bham/plottracer/main/examples/skewed.preview.png)

- The angle is that of the image's long straight lines, within 10° of
  horizontal or vertical, such as the axis lines, a frame and grid lines,
  each measured to a fraction of a pixel along its length. The straight
  stretches of curves are lines too, so it is the angle that most of the
  lines' length agrees on, and only if horizontal and vertical lines agree
  on it, as the axis lines do. On plots turned by 0.25° to 5°, it is found
  within 0.01°.
- An image turned less than 0.1° is read as it is, as it reads as well as
  it would levelled, so the results for level images are as they were. So
  is an image without both a horizontal and a vertical line that agree,
  such as a plot without axis lines; give `--level` for it.
- The image is turned about its middle, onto an image large enough to hold
  all of it, its corners filled in its background colour, and moved by up
  to half a pixel so that the axis lines run along rows and columns of
  pixels, as they were drawn, rather than blurred across two.
- `-l` and `-m` positions are those in the image as given, as are those
  printed when you click, and are turned with it.
- On a framed plot and one with only a left and a bottom axis line, turned
  by 0.25° to 5° either way, no axis line or frame is traced, and the curves
  are as close as in the plots drawn level: within 0.002 of the y-axis's
  span by the median, both from `-p` and `-l` and with the axes read, and
  0.009 at most.
- `--level DEGREES` sets the angle by hand: the angle by which the axes are
  turned anticlockwise, or clockwise if negative. `--no-level` reads the
  image as it is, for a plot drawn turned on purpose.
- Finding the angle takes about 0.02 s for an image of 640 × 400 pixels,
  and 0.1 s for one of 1920 × 1200, as a plot scanned at 300 dpi is; turning
  the image takes about half as long again.

### PDF files

With the `pdf` extra installed, give a PDF file instead of an image, and
plottracer reads the plot on its first page exactly, from what the PDF draws
rather than from pixels. The tick labels and titles are text, so they need no
OCR, and the curves are lines through points whose positions the PDF holds:

```bash
plottracer examples/labelled.pdf --preview labelled.pdf.png
```

For a paper with several figures on one page, start with `plottracer paper.pdf
--page 3`. When plottracer identifies multiple figures, it prints their caption
labels and boxes and asks you to choose. For example:

```bash
plottracer paper.pdf --page 3 --figure S1 -o supplement.csv
```

Plottracer associates labels such as “Fig. 2” and “Supplementary Figure S1”
with nearby vector drawings or images in the same column. It checks for
intervening article text. With one figure or no recognised captions, the
default is the whole page. Use `--box` to select a region manually.


```text
plottracer: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "Voltage (mV)" with ticks at -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1
```

```text
Time (s),Voltage (mV)
-1.11905e-07,1
0.0250124,0.992523
...
```

On the example PDFs, which matplotlib draws, the recovered points lie within
4×10⁻⁷ of the true curves, in the units of the axes, or in decades on
logarithmic ones: the PDF reader holds positions to about seven significant
figures, and the CSV file writes six. Each curve gives the points that it is
drawn through, in the order they are drawn, rather than a point per column of
pixels. Plotting libraries leave out points that lie on a straight line
between their neighbours, as matplotlib does, so a straight line gives only
its ends.

- Positions on a PDF's page are in points, 72 to the inch, from its bottom-left
  corner, as PDF files give them. So are `-l` locations, and those that
  plottracer prints when you click. The preview shows the part of the page
  read, at 2 pixels to the point.
- `--page N` reads another page than the first. On a page with several
  figures, such as a page of a paper, give the part of it to read with
  `--box X0,Y0,X1,Y1`: its left, bottom, right and top edges in points. Text
  outside the box, and lines beyond it, are left out. Figures that a document
  takes in from other PDF files, as LaTeX does, are read too.
- The axes are read as they are from an image (see
  [Reading the axes](#reading-the-axes)), but from the PDF's text, whose
  superscripts and subscripts show by the sizes and heights of their
  characters. The axis lines are the straight lines nearest the tick labels,
  and each tick lies exactly at its tick mark, or its grid line: the mark
  nearest the middle of its label, or, for rotated labels, the end.
- Curves are told apart by the colour, width and dashes they are drawn with,
  as [Several curves](#several-curves) shows, and `-n` ranks them by their
  length. Paths of one style make one curve where each starts at the end of
  the last, or carries it on a little beyond, as the dashes of a line drawn
  dash by dash do. Lines straight along an axis, such as axis lines, tick
  marks, grid lines and the lines in a legend's keys, are left out, as are
  closed shapes, such as frames, filled areas, and points hidden beyond the
  limits of the axes.
- Markers, as in scatter plots and lines with markers, give the points they
  mark, exactly: matplotlib places each marker on the page at its point. A
  colour drawn with markers gives their points, in the order they are drawn,
  rather than the line through them, and `-n` and `-c` tell such series apart
  by colour too: a marker's colour is its fill, or its outline's for open
  markers. With `-n`, or `-c` and a style, a line in that colour that does
  not pass through the markers alone, such as a fit, is a curve of its own,
  after them. Series that share a colour, as in `examples/markers.pdf`, are told
  apart by their markers' shapes with `-m`, given the position in points of
  each series' marker in the legend, or of any of its markers. The markers
  in a legend, with their labels beside them, the caps of error bars and
  the arrowheads of one-sided ones are left out. On `examples/markers.pdf`
  and `examples/scatter.pdf`, every marker is found, even where series
  cross, within 3×10⁻⁶ of its point.
- With `--error-bars`, error bars are read exactly, from the straight
  vertical and horizontal lines that pass through each point, as
  matplotlib draws them. They are matched to the points by where they lie,
  so bars in another colour than their markers are read too, and so are
  the bars of lines drawn without markers. Their ends are the bounds, even
  beyond the limits of the axes; a one-sided bar, as matplotlib draws with
  `lolims` or `uplims`, ends at its point. On `examples/errorbars.pdf`,
  every end lies within 1.3×10⁻⁶ of the truth.

```bash
plottracer examples/markers.pdf \
    -m 108.45,12.91 -m 187.85,12.91 -m 272.06,12.91 -m 347.51,12.91
```

```text
plottracer: x-axis "x" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "y" with ticks at 0, 0.5, 1, 1.5, 2, 2.5, 3
plottracer: markers found in each series: 19, 19, 19, 19
```

- Curves drawn as images, as plots of many points often are to keep their PDF
  files small, are traced as they are in an image, from the image's own
  pixels, and placed where the image lies on the page, while the axes are
  still read from the PDF's text and lines. On `examples/rasterised.pdf`,
  whose curves matplotlib draws as an image at 100 dots per inch, they are
  within 0.006 of the true curves, about half a pixel. `--remove-grid` paints
  over grid lines in such images.

```bash
plottracer examples/rasterised.pdf -n 2
```

```text
plottracer: colours: -c ff7f0e -c 1f77b4
```

### SVG files

Give an SVG file, and plottracer reads it as it reads a PDF file, exactly,
from what it draws: its text, its paths, through their transforms and clip
paths, and its images. It needs no extra. Files compressed with gzip, such as
`.svgz` and `.svg.gz` files, are read too, and a file named otherwise is taken
for an SVG file if it starts with a tag, and an `<svg` tag in its first 4 KiB:

```bash
plottracer examples/labelled.svg
```

```text
plottracer: x-axis "Time (s)" with ticks at 0, 2, 4, 6, 8, 10
plottracer: y-axis "Voltage (mV)" with ticks at -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1
```

The examples `labelled`, `logarithmic`, `series`, `markers` and `rasterised`
are saved as SVG files twice: `name.svg` with its text drawn as glyph outlines,
as matplotlib draws it by default, and `name-text.svg` with
`svg.fonttype: none`, as text. On both, the recovered points lie within
4×10⁻⁸ of the true curves, as matplotlib writes positions to six decimal places
of a point, every marker within 2×10⁻⁸ of its point, and the curves drawn as an
image in `rasterised.svg` as closely as in `rasterised.pdf`.

- Positions in an SVG file are in points from the bottom-left corner of its
  drawing, as in a PDF file: so are `-l` locations and `--box`, which chooses
  part of the drawing to read; `--page` is refused. A user unit, in which an
  SVG file places what it draws, is a CSS pixel, 0.75 points, unless the
  root's `width`, `height` and `viewBox` make it otherwise: matplotlib's are
  points, so its SVG and PDF files share their positions.
- Text is read from `<text>` and `<tspan>` elements, each character placed by
  its `x`, `y`, `dx` and `dy`, through `transform`s, so rotated titles read as
  in a PDF file. Superscripts and subscripts are found by their sizes and
  heights, raised or lowered by `baseline-shift`, or by `dy` as Plotly raises
  its exponents. An SVG file does not give the widths of its characters, so
  each is taken to be 0.6 of its font size wide, 0.7 for capitals, or 0.3 or
  0.9 for narrow and wide ones: a label anchored at its middle or end, as
  tick labels usually are, lies exactly where it is drawn, and its tick at
  its tick mark.
- matplotlib's text drawn as glyph outlines is read from the comment that
  matplotlib writes before each line of it, in a group whose id starts
  `text_`, with mathtext such as `$\mathdefault{10^{-1}}$` written as plain
  text, `10⁻¹`, and the box of the outlines. So is its mathtext written as
  text, which matplotlib writes a character at a time, grouped by font rather
  than in the order they are read.
- Markers drawn with `<use>`, as matplotlib draws them, mark the point at the
  `x` and `y` of the `<use>`, as a form marks its origin in a PDF file; other
  markers, such as `<circle>` elements or Plotly's paths of arcs, mark the
  middles of their boxes. Arcs pass through their points at each quarter turn
  of their ellipses, as do circles and ellipses.
- Colours come from presentation attributes, such as `stroke="#1f77b4"`, and
  `style` attributes, as elements inherit them, in hexadecimal, `rgb()` or by
  name, including `currentColor`; `opacity` and `stroke-opacity` of 0 hide a
  path. Each path keeps its stroke width and dash pattern, and a gradient
  paints in its first colour.
- Images, given as data URIs or as files beside the SVG file, are traced as in
  a PDF file, placed and turned as they are shown: matplotlib draws them
  upside down and turns them over.
- The preview is drawn by plottracer itself, with OpenCV: the paths, the
  images and the text, in a font of its own.
- Files that declare XML entities are refused, as their entities can expand
  to fill the memory: the "billion laughs" is refused in a tenth of a
  millisecond. So are files of more than 64 MiB, decompressed, and those
  that draw more than a million elements, counting each `<use>`.

### Python

The same steps are available as a library:

```python
from plottracer import Calibration, load_image, trace_curve

image = load_image("examples/spectrum.png")
calibration = Calibration.from_points(
    data=[(0, 0), (10, 0), (0, 1)],
    pixels=[(80, 44), (576, 44), (80, 147)],
)
data = calibration.to_data(trace_curve(image, calibration.origin))
```

or, with the `ocr` extra, reading the axes:

```python
from plottracer import load_image, read_axes, trace_curve

image = load_image("examples/labelled.png")
axes = read_axes(image)
data = axes.calibration.to_data(trace_curve(image, axes.calibration.origin))
print(axes.x_title, axes.y_title)  # Time (s) Voltage (mV)
```

For logarithmic axes, pass `x_log=True` or `y_log=True` to
`Calibration.from_points`; `read_axes` sets them itself.

`read_panels` reads each panel of a figure, in reading order. A `Panel` has
its `label`, such as `"a"`, or None; its `image`, the panel's part of the
image, in which to trace its curves, with its label painted over; its `axes`,
in pixels of its image; the `offset` of its image's bottom-left corner in
the whole image; and `inset_of`, the panel that an inset lies in, or None.
An image of one plot is one panel, of the whole image, and each inset
follows the panel it lies in, without a label:

```python
from plottracer import load_image, read_panels, trace_curve

for panel in read_panels(load_image("examples/panels.png")):
    calibration = panel.axes.calibration
    data = calibration.to_data(trace_curve(panel.image, calibration.origin))
    print(panel.label, panel.axes.x_title)  # a Energy (eV), b Time (s), ...
```

For several curves, `find_colours` returns the colours of the most common
curves as `(red, green, blue)` tuples, and `trace_curves` traces one curve per
colour. Pixel positions are those of pixel centres, and a curve that crosses a
column more than once gives a point for each crossing, sorted by x, then y:

```python
from plottracer import find_colours, trace_curves

colours = find_colours(image, 4, calibration.origin)
curves = trace_curves(image, colours, calibration.origin)
data = [calibration.to_data(curve) for curve in curves]
```

`find_series` returns every curve as a `Series`, telling those that share a
colour apart by style and where they run, and `trace_series` the curves of
each of the given colours. A `Series` has its `colour`; its `style`, one of
`STYLES`: `"solid"`, `"dashed"`, `"dotted"` or `"dashdot"` for a line, or
`"markers"`; its `width`, that of its line or the size of its markers, in
pixels, or in a PDF file in points; its `points`; and its `name`, such as
`040404-dashed`:

```python
from plottracer import find_series, load_image

for series in find_series(load_image("examples/styles.png"), (80, 44)):
    print(series.name, len(series.points))  # 040404-solid 493, ...
```

For series told apart by their markers, `find_markers` takes the pixel
position of each series' legend key, and returns the middle of each marker of
that shape:

```python
from plottracer import Calibration, find_markers, load_image

image = load_image("examples/markers.png")
calibration = Calibration.from_points(
    data=[(0, 0), (10, 0), (0, 1)],
    pixels=[(55, 83), (627, 83), (55, 185)],
)
keys = [(148, 18), (260, 18), (377, 18), (483, 18)]
markers = find_markers(image, keys, calibration.origin)
data = [calibration.to_data(series) for series in markers]
```

`find_marker_series` takes the same arguments and gives more: for each
series, a `MarkerSeries` with its markers' `points`, whether each is
`ambiguous`, and where its markers are probably `hidden`, in pixels. Keys
whose markers look alike at the image's size give a `UserWarning`.

`find_error_bars` finds the filled markers of each colour and their error
bars, as `--error-bars` does, and returns an `ErrorBars` for each colour:
its `points`, the middles of the markers, and the `low` and `high` ends of
their bars, as rows of x and y like the points': the x of the left end of
each marker's horizontal bar and the y of the lower end of its vertical
one, then the right and upper ends, NaN where a marker has no such bar, or
its end cannot be seen. On a reversed axis, the lower end is the larger
value:

```python
from plottracer import find_error_bars

for bars in find_error_bars(image, colours, calibration.origin):
    points = calibration.to_data(bars.points)
    low, high = calibration.to_data(bars.low), calibration.to_data(bars.high)
```

`find_marker_error_bars(image, keys, origin)` does the same for the
markers that `find_markers` finds.

`load_image` returns an RGB array, and `remove_grid(image)` paints over grid
lines first, if needed.

`level_image` levels an image whose axes are turned, as the command does. It
returns the levelled image, and a `Levelling`, whose `angle` is the angle the
image was turned back by, or 0 if it was left as it is, and whose `points`
and `back` methods find where pixel positions lie in the levelled image, and
back:

```python
from plottracer import Calibration, level_image, load_image, trace_curve

image, levelling = level_image(load_image("examples/skewed.png"))
pixels = levelling.points([(82.8, 39.8), (578.7, 48.5), (81.0, 142.5)])
calibration = Calibration.from_points([(0, 0), (10, 0), (0, 1)], pixels)
data = calibration.to_data(trace_curve(image, calibration.origin))
```

`level_image(image, angle)` turns the image back by a given angle, and
`find_skew(image)` returns the angle alone.

With the `pdf` extra, `load_pdf` reads what a page of a PDF file draws, and
the same steps work on it, in points from the page's bottom-left corner; so
does `load_svg`, for an SVG file, without the extra:

```python
from plottracer import find_pdf_colours, load_pdf, read_pdf_axes, trace_pdf_curves

drawing = load_pdf("examples/series.pdf", page=1)
axes = read_pdf_axes(drawing)
origin = axes.calibration.origin
curves = trace_pdf_curves(drawing, find_pdf_colours(drawing, 4, origin), origin)
data = [axes.calibration.to_data(curve) for curve in curves]
```

`load_pdf` takes the part of the page to read as `box`, a tuple of its left,
bottom, right and top edges in points. `render_pdf(drawing)` draws that part
as an image, at 2 pixels to the point, and `remove_pdf_grid(drawing)` paints
over grid lines in the images that it draws. `find_pdf_markers(drawing, keys,
origin)` finds series by the shapes of their markers, as `find_markers` does
in an image, from the position of a marker of each series in points.
`load_svg(path, box)` takes the part of an SVG file's drawing to read in the
same way, and `render_svg(drawing)` draws what it read, with OpenCV.

`find_pdf_series(drawing, origin)` returns every curve and series of markers
on the page, longest first, each as a `Series`, told apart by how it is drawn
as well as by colour, and `trace_pdf_series(drawing, colours, origin)` the
series of each colour:

```python
from plottracer import find_pdf_series, load_pdf

for series in find_pdf_series(load_pdf("examples/styles.pdf"), (57.6, 31.68)):
    print(series.name, series.width)  # 000000-dotted 1.5, 000000-dashed 1.5, ...
```

`find_pdf_error_bars(drawing, points)` finds the error bars through the
points of each series, such as `trace_pdf_curves` or `find_pdf_markers`
returns, exactly, as an `ErrorBars` for each.

## How it works

Before following a curve, the image tracer checks for horizontal and
vertical reference strokes that reach across the axes, including regular
dashes and dots. It excludes their visible ink and prints the calibrated
coordinate and colour. A lone flat stroke has no independent data beside
it, so the reference classifier leaves it as data. A PDF supplies its
clipped reference path coordinates directly.

To inspect the classification, run `plottracer figure.png --classes objects.png`.
The image has a colour key, and the terminal gives a count for each class.
Lines, marker groups and error-bar groups are counted per detected series.
Regularly spaced parallel rules appear as gridlines. Broad fills can appear as
shaded bands or background spans. A matching legend label can identify a mean,
replicate, fitted or theoretical curve. When two labels share an ink,
a continuous sample and a broken sample can distinguish their strokes. An unlabelled stroke is a **data line**.
A line passing through measured marker centres is a connecting line. Isolated
arrows and brackets can be recognised from a narrow shaft with two wings
or a transverse cap at each end. The view
uses levelled raster coordinates. PDF and SVG views use two pixels per page
point and native text and legend positions. Vector CSV coordinates and
reference values come from the original drawing.

In Python, `classify(image, origin, calibration=calibration)` returns objects
with a kind, measured colour and local pixel mask. Pass them to
`trace_curves(image, colours, origin, objects=objects)` to apply the exclusions.
Reference objects carry their axis, pixel coordinate and calibrated value.

1. The image is read in colour and stretched to the full 0–255 range.
   Transparent pixels become white. If its axes are turned, it is levelled:
   lines at least a fifth of the image's shorter side long, within 10° of
   horizontal or vertical and at most 8 pixels thick, are found in its ink
   with a probabilistic Hough transform. In each column across a line close
   to horizontal, or each row across one close to vertical, the line's middle
   is the mean position of the ink within 4 pixels of it, weighted by its
   darkness, and the line's angle that of the straight line through its
   middles by least squares, leaving out those where other ink meets it. The
   angle that most of the lines' length agrees on, within 0.05°, is the
   image's, if both horizontal and vertical lines agree on it, and if it is
   0.1° or more, the image is turned back by it with Lanczos interpolation.
2. A least-squares fit through the reference points gives a linear map from
   pixels to data for each axis, or to the logarithm of the data for a
   logarithmic axis. The leftmost and lowest reference points mark the axes.
3. Pixels left of and below the axes are ignored, which drops the axes, their
   ticks and labels. So are axis lines up to 12 pixels inside the axes
   given: rows or columns of ink near them that run unbroken from the other
   axis across 60% of the plot. A frame closing the axes, a thin line along
   the top half from the y-axis that meets one down the right, is dropped
   with everything beyond it. Frames inside what remains, such as those of insets and
   legends, are painted in the background colour with all they hold, and 3
   pixels around them, so an inset's curve or a legend's key is not traced
   as part of a curve of its colour. A frame is a box of four straight lines
   of one colour, each at least 5% of the plot's shorter side long, at most
   8 pixels thick, and 32 levels or more from the background, so light grey
   legend frames count too. The lines along its top and bottom run from one
   side to the other, and a line down each side runs along three quarters
   of its height or more, so its corners are square or rounded by a few
   pixels. Lines may run on past its corners, as an inset's tick marks and
   the lines to the part it magnifies do, but the top and bottom must each
   end at a side, which grid lines do not. A step plot's flat and upright
   stretches never close a box, and filled areas, such as bars and shaded
   spans, are thicker than lines. A frame hides what lies behind it, so a
   box that a curve crosses, with ink of like colours, less than 48 levels
   apart, within 3 pixels of a line on both sides, is not one, such as a
   box drawn around part of a curve or marking the part an inset magnifies.
   An inset's tick marks, in the colour of its frame, do not cross it where
   its curve meets the frame inside, nor do its grid lines.
4. A coarse colour histogram gives the common colours: those with enough pixels
   to span the plot, merging shades less than 48 levels apart. Each is then
   counted again in a finer histogram, and split if it holds two or more
   colours with enough pixels, such as neighbouring colours of a colour map.
   The most common is the background, unless the colour along the top of the
   plot differs from it by 64 levels and has at least a third as many pixels:
   then the most common colour is a filled area. Antialiasing blends the edges
   of each curve with the background, and a blend can be common too, so a
   colour whose pixels mostly touch those of a colour further from the
   background is dropped. So is a colour whose pixels show several inks along
   separate lines, as the pale blends of thin lines of several colours do
   when counted as one: a pixel's ink is its direction from the background,
   which antialiasing and transparency leave as it is, and directions less
   than 6 degrees apart are one ink's.
5. A thin line never reaches its full colour, and its shades are each too
   rare to count. So the pixels that show some ink, 16 levels or more from
   the background, that no common colour claims, other than the pale edges
   of its own pixels, are taken by their inks, and an ink whose pixels,
   weighted by their strength, add up to half a pixel for each column of the
   plot at full strength is a colour too. Each colour is then the strongest
   its pixels show along its direction, but for a fiftieth of them, so an
   opaque line gives its own colour, and a thinner one its strongest shade.
6. The curves are the colours that differ from the background by at least
   64 levels in some channel, most pixels first, except those whose pixels
   lie mostly more than 4 pixels inside their shapes, which fill areas; those
   drawn mostly as straight lines across 60% of the plot, as grid lines are;
   pale strips on the same ink ray at least 2.5 times as deep as a stroke
   that they border along more than a third of its edge; and overlaps
   surrounded mostly by two colours and showing 0.1 to 1.25 times each
   colour’s strength. Depth is measured before removing antialiased edges. `-c` instead picks the colour nearest to each given colour, up to
   96 levels away, or failing that the nearest whose direction lies within
   12 degrees of its own, no further from the background: a thin or
   translucent line of that colour. When a thin segment lies inside a broad fill, plottracer
   checks the adjacent stroke outside the fill. A matching mixture of the
   stroke and fill colours joins the two segments.
7. Each pixel belongs to the colour that it best matches when blended
   with the background, if it is at least half-way there, so a curve's
   antialiased edges count towards it, and within 96 levels of the blend, so
   ink of other colours, such as the black text and arrows of annotations
   among coloured curves, is not taken for the nearest curve. A pixel of a
   colour that touches those of a stronger colour of the same ink, within 12
   degrees, is that colour's edge, such as the pale grey edge of a black
   curve, and belongs to neither. Specks of fewer than four pixels, such as
   where two other curves cross, are ignored.
8. A colour's filled markers are its shapes whose middles lie a pixel or
   more deeper inside it than the middles of its lines, and are wider and
   taller there than a line, as where lines cross they are not; a marker's
   box is that of its middle, widened by how deep that lies. Its error bars
   run from the edges of its box along its middle column and row, while
   the colour lies within a line's width of them, across ink of other
   colours up to 6 pixels wide, and most of their rows are no wider than a
   line. A bar ends where the colour does, or at the middle of its cap: its
   last rows, no more of them than the bar is wide, wider than the bar and
   centred on it within a pixel. Without a cap, a bar must run 5 pixels,
   with the middles of its rows within a pixel of one another, and must
   not end where a line of the colour leaves its path, within 2 pixels of
   it. The bars and caps are left out of the colour before its curve is
   traced, and with `--error-bars` their ends are the bounds; an end
   hidden by another colour or the edge of the plot is not known.
9. In each image column, a curve's pixels form runs, split by gaps of more
   than 6 pixels, and runs that overlap in neighbouring columns are followed
   as tracks. Where runs merge or part, as where curves cross, each track
   carries on the way it was headed, fitted to its last six points where its
   line was clear of others. Each run of a track across at least 5% of the
   plot gives a point at its median height, so both sides of a loop are
   traced, and shorter pieces beside a longer track, such as the caps of
   error bars, are ignored. A column without such a run gives one point at
   the median height of all its pixels.
10. To tell the curves of a colour apart, tracks also carry on across gaps:
   up to 12 line widths, the median height of the colour's runs, turning by
   at most 25°, or, from a dot, which heads no way, up to 3 line widths
   whichever way. Another colour covering most of the gap also lets a
   track continue through a crossing, and the covered part does not count
   as a gap when telling solid from dashed lines. A track that shares a run holding one line, headed a line
   width beyond it, waits for its next dash. Tracks that share a run keep
   their order while it is more than one and a half lines tall, as where
   curves touch, each at the edge on its side, and otherwise share its rows
   by where they are headed. Tracks across 5% of the plot are curves, those
   that part or meet heading 90° apart or more, as the sides of a loop, are
   one, and so are those where one starts as another ends. Markers come
   out first: three or more blobs of like size, within a factor of two, whose
   cores lie deeper inside than half the line's width and 1.5 pixels more,
   taking the line's width to be the height that a quarter of the colour's
   runs reach. A colour with markers and a curve gives their middles as a
   series of its own, before its curves. A curve inked
   along 90% of its length is solid; otherwise it is dotted if three quarters
   of its dashes are at most two line widths long, less the width, dashed if
   three quarters are longer, and dashdot if neither.

With `-m`, steps 4 to 10 give way to these:

1. Each key's colour is the direction, seen from the background, of the
   strongest ink of the piece nearest the key's position, and keys whose
   colours lie 10 degrees apart or less share one. Each pixel shows the key
   colour whose direction lies nearest its own, if 20 degrees away or less,
   and none of the others, so series of one shape are told apart by colour,
   and ink of other colours is left out.
2. Each key's marker is the piece of ink in its colour, 64 levels or more
   from the background, nearest the key's position, leaving out specks of
   fewer than four pixels. A key at least 1.5 times as wide as tall may have
   a line through its marker: the rows with ink within 3 pixels of its ends.
   It does if they are fewer than the marker's rows, the other rows with ink
   at least half as strong as the strongest, as compression leaves fainter
   ink beside the line. The line is masked out of the marker, as lines cross
   the markers in the plot at other angles, except where the marker hides
   it, differing from the line's level by more than a quarter of the range,
   as an open marker's face does. Keys of one colour whose markers, fitted
   to each other as in step 4, differ by less than 5% look alike, and give a
   warning.
3. A marker lies where a key's marker correlates with the plot by 0.6 or
   more, and the plot has at least 60% of the ink of each half of the
   marker, top, bottom, left and right, so a thin marker, such as a cross,
   is not found along a line. Matches are taken best first, leaving out those
   less than three quarters of a marker's width from a marker already found.
4. Where other keys' markers score within 0.05 of a match, up to 2 pixels
   away, each is fitted to the plot, shifted by the match's fraction of a
   pixel, where it differs least by the normalised sum of squared
   differences, which, unlike a correlation, tells circles from octagons.
   With it, up to two markers that it partly hides, or that partly hide it,
   are found, as in step 5, and it is moved to fit what they leave
   uncovered. The marker that, with those, differs least from the plot
   wins, and is ambiguous if another differs less than 30% more.
5. Markers partly hidden under those found, or over them, are then looked
   for around each, in what all those found leave uncovered: what shows
   must hold a fifth or more of the key's ink, correlate with the key's
   marker by 0.75 or more, and explain the plot under it at least 60%
   better than the markers found alone.
6. Series usually share x positions. Where two of a series' markers lie
   further apart than one and a half times their usual spacing, and other
   series have markers at an x between them, the series' marker would lie
   there, on the straight line between its two. Each overlapping marker is
   checked in turn, nearest first. A match must explain the pixels better
   and keep the markers within two pixels of the shared x. Otherwise the
   missing marker is reported as probably hidden.
7. Each marker's middle is placed to a fraction of a pixel.
8. With `--error-bars`, each marker's box is the size of its key's marker,
   and its bars are followed as in step 8 above, in ink of any colour, 64
   levels or more from the background.

On the examples, the recovered values are within 0.03 of the true curves, about
three pixels at the steepest point, and the markers within 0.02 of the points
they mark, about two pixels.

With a PDF file, steps 1 to 10 give way to these:

1. [PDFium](https://pdfium.googlesource.com/pdfium/), through pypdfium2, reads
   the characters, paths and images that the page draws, each with the matrix
   that places it on the page, through any forms that hold it. Characters
   along a line less than 0.6 of their font size apart make a text, with a
   space where they lie 0.2 of it apart or more. Characters at most 0.85 the
   size of their line's largest, and 0.15 of its size or more above or below
   it, are superscripts or subscripts.
2. The tick labels and titles are read from the texts as they are from those
   that OCR reads. The x-axis line is the lowest straight horizontal line
   above the x-axis tick labels that runs along 90% of their span, and the
   y-axis line likewise the leftmost vertical one right of the y-axis's.
3. The tick marks are the straight lines across an axis line that reach to
   within a tenth of a label's height of it. Labels whose middles lie within
   half a label's height of the marks are centred on their ticks; others are
   aligned by the ends nearest the marks, as rotated labels are. Each tick
   lies at the mark nearest its label's middle or end, within a quarter of the
   tick spacing.
4. The curves are the paths that are stroked but not filled, without their
   subpaths that are closed or that lie along a straight horizontal or
   vertical line. Points left of or below the axes, or outside the path's
   clip, are left out. Curved pieces give their ends, not their control
   points. Curves are told apart by the colour, width and dashes of their
   paths, scaled to the page, and named by their dashes: solid without gaps,
   dotted if each dash is at most twice the width long, dashed if none is,
   and dashdot if some are. A path carries on the curve of its style whose
   last path it starts at the end of, or at most three times the longer
   one's length beyond, in a direction turning by at most 30° from both;
   others are curves of their own. With `-c` and a colour alone, every curve
   of that colour is one. Curves are ranked by the length of the lines
   through their points.
5. Markers are shapes at most 36 points across, other than a straight
   horizontal or vertical line, drawn at least twice. A form that holds one,
   as matplotlib draws them, marks the point where its origin lies; a marker
   drawn as a filled or closed path marks the middle of its box. Markers with
   a text level with them, right of and above the axes, beginning or ending
   within four of its heights, are a legend's keys and left out. A colour
   drawn with markers gives their points in place of its lines, a series for
   each shape and fill, ranked by the length of the line through them; with
   `-n`, a line in its colour that passes through any point other than the
   markers' centres, within 0.01 points, comes after them, as a curve of its
   own. With `-m`, the marker nearest each position, within 10 points, picks
   those of the same colours whose outlines lie within 0.01 points of its own.
6. Each image drawn on the page, with any transparency on white, is traced as
   an image of a plot from where the axes cross in it, or from its edges if
   they cross outside it, and its curves are ranked with those drawn as paths.
7. With `--error-bars`, a point's error bars are the straight vertical and
   horizontal lines, stroked and not filled, that pass through it to within
   0.01 points, other than those that run from one edge of their clip to
   the other, as grid lines do. A bar through several points belongs to
   those with a marker drawn in its colour, if any, and a point with
   several bars one way takes the one through it alone, or else the one in
   its marker's colour, or none. A shape lying wholly beyond one end of
   such a line, whose other end lies at a marker, is the arrowhead of a
   one-sided bar, not a marker.

With an SVG file, step 1 gives way to this:

1. Python's XML parser reads the file, refusing it at the first declaration
   of an entity. Each element is drawn through the transforms of those that
   hold it, and the viewBoxes of the `<svg>` elements, into points from the
   bottom-left corner, taking the properties it inherits from them. A
   `<use>` draws its target with its origin at its `x` and `y`, as a form
   marks a marker in a PDF file. A path passes through the ends of its
   pieces, and an arc through the points at each quarter turn of its
   ellipse; its clip is the box around its clip path's shapes, within those
   of the elements that hold it. The characters of `<text>` and `<tspan>`
   elements are placed after one another, 0.6 font sizes apart, 0.7 for
   capitals, or 0.3 or 0.9 for narrow and wide ones, unless an `x` or `y`
   places them, and each run of them is moved back by half its width or all
   of it if anchored at its middle or end, then joined into texts as a PDF
   file's are. Each line that matplotlib writes after a comment, as glyph
   outlines or as mathtext a character at a time, is a text of its own: the
   comment, with its mathtext written plainly, in the box of its outlines or
   characters.

## Accuracy

Across 100 ChartX line charts and 102 PlotQA dot-line charts, plottracer
scored 0.322 mean numeric F1 with automatic axes and a supplied series
count. On the PlotQA subset, supplying tick calibration raised the score
from 0.230 to 0.553. These figures cover the paired public items only.
The benchmark gives per-chart results, error percentiles, failure counts
and the comparison with other systems.
[Public chart benchmark](https://github.com/SMTG-Bham/plottracer/blob/main/benchmarks/README.md).

## Limitations

Use `--trace-range` to inspect the vertical stroke extent of a raster curve,
especially near a steep peak.


For images up to 400 pixels on the longer side, the CLI reports how much one
pixel spans on each axis. A logarithmic axis reports a multiplier per pixel.
When the first OCR pass finds too few ticks, plottracer looks for long axis
lines, isolates their labels and enlarges each label for recognition. It
traces curves at their original resolution.

On the labelled 40 dpi test plot (256 × 160), the tick digits are 4–5 pixels
tall and the nominal line is 0.83 pixels wide. All six x ticks and nine y
ticks were recovered. The largest curve error was 1.33% of the y range,
compared with 0.62% at 50 dpi. On three local runs after loading the OCR
engine, the second calibration pass added 0.46–0.99 seconds. Text near
this size can still be misread, including axis titles.


- In an image, curves of one colour are told apart by their dashes and
  where they run, not by the widths of their lines. Curves that run together
  for long stretches, or touch so closely that their lines overlap by more
  than half, may swap where they meet, as may a solid curve where the dash
  of another curve of its colour lies along it as they cross, if the lines
  are thick. Of two curves drawn from one point, the one that parts from the
  other starts where they part. Translucent curves, such as pale replicates
  about their mean, darken where they cross, so they break into pieces there,
  each a series of its own, often named `dashed`. Series told apart by their
  markers give only the markers that can be seen, not the lines between them,
  and each key's marker is taken to be of one colour, that of its strongest
  ink. In a greyscale image, curves of one grey are told apart only by their
  dashes and where they run, and a lighter curve loses a few pixels where it
  touches a darker one. Greyscale dot screens with two perpendicular repeats
  of about 2.2 to 9 scan pixels can be detected when the plot contains a
  textured patch at least 128 pixels square. Irregular dithering and finer
  screens can still break a curve into dots.
- A curve needs enough pixels to span the plot, so short curves may not be
  found, and dots of fewer than four pixels are ignored, so sparse dotted
  lines may not be either.
- Where curves cross, the one drawn underneath loses a few pixels, and its
  points there can be a pixel or two out.
- The axes must be horizontal and vertical, or turned alike by up to 10°, as
  in a scan, which plottracer levels (see [Turned images](#turned-images)),
  with linear or logarithmic scales, though either may be reversed. To read
  the axes, the x-axis must lie below the plot and the y-axis left of it. A
  photo of a page or a screen, in perspective, is not made square, nor is a
  page scanned sideways turned upright.
- An image is levelled only if it has long straight lines both horizontal
  and vertical that agree on its angle, such as the two axis lines: a plot
  without axis lines, or one with only an x-axis line and horizontal grid
  lines, as many spreadsheet charts draw, is read as it is, unless given
  `--level`. Images in PDF files are not levelled.
- Levelling an image blurs its lines a little, as each pixel is made from
  several. A dark grid's lines then vary in shade along their length, and
  can be traced as a curve: give `--remove-grid`. Markers drawn in thin
  lines, such as crosses, can be missed with `-m`, or found a pixel or two
  off.
- Each column gives one point per branch of a curve, so steep peaks come out
  slightly lower, vertical lines in the plot area become single points, and
  the two sides of a loop merge into one point where they come within 6
  pixels of each other.
- `--remove-grid` also removes straight horizontal or vertical stretches of
  curves that are 120 pixels or longer.
- Panels are found by axis lines that meet, so a figure of plots without
  them, or of plots whose axes touch, as plots sharing an axis can, is read
  as one plot. Shared legends can identify matching series across panels.
  Vector panels with distinct clipping regions and a shared legend can be read
  together. Use `--box` to select other vector layouts.
- An inset needs a frame of four lines that
  stands clear of the axes' own frame: an inset in the corner of the axes,
  sharing two of their lines, is traced as part of the plot, and not read as
  a panel of its own. So is an inset whose curve meets its frame where a
  curve of a like colour runs behind it, as the two look like one curve
  crossing a box. The tick labels and titles of an inset, outside its frame,
  can remain in the plot area when OCR misses them. `--remove-grid`
  removes the lines of an inset's frame 120 pixels or longer before the
  frame is found, so give it only for plots without insets. In a PDF file,
  insets are not left out: their curves are read with the plot's.
- An inset is read as a panel of its own only if OCR reads three or more
  tick labels on each of its axes, from the text read across the whole
  panel. A curve of the plot that runs through an inset's tick labels can
  hide them from OCR, and a small inset may have only two on an axis; the
  inset is then painted out, unread, without a warning, as a legend is.
- In a PDF file, the text must be text to read the axes: text drawn as
  outlines, as some programs export it, has none to read, so give `-p` and
  `-l`. So it must be in an SVG file, other than matplotlib's glyph outlines:
  glyphs that cairo draws, in `<use>` elements of targets whose ids start
  `glyph`, are left out.
- In an SVG file, only presentation attributes and `style` attributes give
  colours: `<style>` sheets are not read, so a file styled by classes, as
  Adobe Illustrator writes one, is read with SVG's defaults, filled in
  black. Filters, masks and patterns are not read, and a gradient is its
  first colour. A clip path clips to the box around it, and a label that is
  not anchored at its middle or end lies up to a few tenths of a font size
  from where it is drawn, as its characters' widths are estimated.
- In a PDF file, lines that are straight along an axis are left out, as grid
  lines are, so a constant curve drawn as a single horizontal line is too.
- In a PDF file, a colour drawn with markers gives with `-c` and its colour
  alone only the points they mark, so a line of the same colour, such as a
  fit, is left out; `-n`, or `-c` with the style `solid`, gives it. In an
  image, markers are told from a line of their colour only if they are
  filled and three or more, and at least 1.5 pixels thicker than half the
  line: open markers and crosses are traced as part of the line, and a
  colour of markers alone gives a point per column, as before, not one per
  marker. A shape drawn only once is not taken for
  a marker, markers each in a colour of their own, as a colour map draws
  them, make series of one point each, and a marker with a text level with it
  close by, right of and above the axes, is taken for a legend's key.
- A plot drawn turned on its page, as on a landscape page that the PDF shows
  turned with its `/Rotate` setting, is read as drawn, and its axes cannot be
  read.
- In an image, error bars are read only through filled markers, or with
  `-m`, so the bars of open markers, without `-m`, and of lines drawn
  without markers are not read, and are traced as part of their curves.
  Nor are bars in another colour than their markers, or horizontal bars
  along a line that runs level through its markers. A bar that runs beside
  a steep line of its colour can run on along the line, and end in the
  wrong place; check the preview. The arrowheads of one-sided bars, as
  matplotlib draws with `lolims` or `uplims`, are taken for markers of
  their own.

## Development

The project uses [uv](https://docs.astral.sh/uv/) 0.13 or newer, which
installs the Python version pinned in `.python-version` if it is missing.
uv prefers its own Python builds, so if the only uv-managed Python 3.14 is the
free-threaded `3.14t`, it picks that one and tries to compile OpenCV from
source; run `uv python install 3.14` once to add the standard build, which uv
then prefers. Create the environment and install the Git hooks once:

```bash
uv sync
uv run prek install
```

The hooks run Ruff, lockfile, workflow security and file hygiene checks before
each commit. Run the complete local checks with:

```bash
uv run prek run --all-files
uv run mypy
uv run pytest
uv build
```

Tests must keep full branch coverage. The tests replace OpenCV's windows with a
fake, so they run without a display. They check the recovered data against the
curves that `examples/make_examples.py` draws; after changing that script,
redraw the examples with `uv run examples/make_examples.py`, which also prints
their `-l` options.

`examples/benchmark_colours.py` measures how well plottracer finds and traces
several series by colour, over plots of 2 to 12 series in several palettes,
line widths, resolutions and JPEG qualities, with and without grids and
legends. Run it with `uv run --with matplotlib examples/benchmark_colours.py`,
which takes about 15 seconds, before and after changing how colours are
found.

`pyproject.toml` declares the oldest supported dependency versions and
`uv.lock` pins the versions used in development and CI. Dependabot refreshes
the lockfile, hooks and actions monthly.

Every push to `main` and every pull request runs the hooks and mypy, the tests
with the locked and the oldest allowed dependencies, and a build with smoke
tests of the sdist and wheel. Require the **All checks passed** job in branch
protection.

### Releasing

Publishing a GitHub release publishes plottracer to PyPI: `release.yml` checks
that the tag matches the package version, builds the sdist and wheel, and
uploads them with trusted publishing. Its header says what PyPI and the
repository settings need for that.

1. Enable the repository in the release owner's [Zenodo GitHub settings](https://zenodo.org/account/settings/github/)
   before publishing the first release.
2. Bump the version, for example `uv version --bump minor`. Update the
   version and `date-released` in `CITATION.cff`, and update `CHANGELOG.md`.
3. Publish a GitHub release tagged with the version prefixed by `v`, such as
   `v0.2.0`. After Zenodo archives the first release, add the concept DOI for
   all versions to the Citing section.

## Project layout

```text
.
├── .github/                 # CI, the PyPI release and Dependabot
├── examples/                # example plots, their previews and the script that draws them
├── src/plottracer/
│   ├── axes.py              # reading the axes from their tick labels
│   ├── panels.py            # finding the panels of a figure and their insets, and reading each
│   ├── calibration.py       # pixel-to-data mapping
│   ├── digitize.py          # image loading, grid removal, curve colours and tracing
│   ├── errorbars.py         # following error bars from markers in images
│   ├── level.py             # levelling images whose axes are turned
│   ├── interactive.py       # window for clicking on reference points
│   ├── pdf.py               # reading plots from PDF files, from their drawing
│   ├── svg.py               # reading plots from SVG files, from their drawing
│   └── cli.py               # the plottracer command
└── tests/
```

## License

plottracer is free software under the GNU Lesser General Public License,
version 3 or later: see
[`LICENSE`](https://github.com/SMTG-Bham/plottracer/blob/main/LICENSE), which
supplements the GNU General Public License in
[`COPYING`](https://github.com/SMTG-Bham/plottracer/blob/main/COPYING). It is
a modified version of [PlotDigitizer](https://github.com/dilawar/PlotDigitizer)
by Dilawar Singh, rewritten in 2026.
