Plotting a CSV file with Typst and CeTZ-Plot
Whenever I need to plot some data, I usually prefer to have a tool that
- is fast,
- is easy to install,
- is reliable,
- is flexible,
- is free to use,
- produces high-quality plots, and
- doesn't require many dependencies.
gnuplot and matplotlib are popular choices, but I personally don't like the appearance of gnuplot and I usually am not so happy with Python's large amount of dependencies.
For quick plotting, I recently discovered CeTZ-Plot. It's a plotting library inside Typst. Typst is a modern alternative to LaTeX, so it is meant to create full documents, but it's also quite easy to use it to create images.
To do this, you only need to have Typst installed. The easiest way to do this is to have the Rust toolchain installed on your system and then run:
Or see the official installation instructions.
In this blog post, I'm using typst version 0.13.1.
As an example, I created some CSV data with the width and height of some apples and pears.
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
,,
Next, I loaded the data into Typst and used cetz-plot to plot it.
Here, Typst will automatically download the cetz and cetz-plot packages when you run this file.
// Load the data from a CSV file.
// Store the width and height in separate variables.
// This is used to override the default axis limits.
// Store the data for each fruit in a separate variable.
// Used to turn the plot into a scatter plot.
To create a SVG from this Typst file, you can run:
Which gives the following SVG file:
For development, in VS Code or Cursor, you can use the Tinymist Typst extension to get syntax highlighting, a language server, and live previews.
With the extension, you can have the plot open in a preview window to see the changes live.
Changes are visible almost instantly.
Generating this SVG image took 0.2 seconds according to time.
If you don't want to use VS Code or Cursor, then you can use the typst watch command to automatically compile the file when it is saved.
To have live-updates, you can also use a PDF viewer than supports live updates such as TeXShop (works on MacOS) or Okular.
For faster reloads, you can also output the pages to SVG or PNG and generate a HTML page that refers to the plots.
That should work if you disable the cache in the developer tools.
Then finally, let's show some more variations of the plot. Here is a dark version with the "left" axis style:
// Add same preamble as before (everything before the canvas).
And one without an axis and with the first two colors from the Wong Color Palette:
// Add same preamble as before (everything before the canvas).