Plotting in Zig: A gnuplot wrapper with similar api as matplotlib from python

A Gnuplot Wrapper with Python’s Matplotlib like Bindings:


This is a personal project that I initially didn’t intended on publishing, but thought might be useful to wierdos like me. As obvious its a gnuplot wrapper with bindings similar to matplotlib. Since I am relatively new to zig and inteded to use this project as a learning project where I learn the heres and theres of the language while building something meaningful, so its highly personalized and unoptimized (just a disclaimer).

Example

const std = @import("std");
const gnuplot = @import("gnuplot");
const Io = std.Io;

const Figure = gnuplot.Figure;

pub fn main(init: std.process.Init) !void {
    const io = init.io;
    const gpa = init.gpa;

    var fig = Figure.init(io, gpa, .{.term = "qt", .width = 1024, .height = 750});
    defer fig.deinit();

    const x = [_]i32{1, 2, 3, 4, 5};
    const y = [_]i32{1, 2, 3, 4, 5};
    const y2: @Vector(5, f64) = .{1.0, 2.0, 3.0, 4.0, 5.0};
    try fig.xlabel("x", .{});
    try fig.ylabel("y", .{});
    try fig.grid();
    try fig.plot(&x, &y, .{.label = "line 1"});
    try fig.scatter(&x, &y, .{.label = "points 1", .color = "red"});
    try fig.bar(y2, .{.label = "bar"});
    try fig.show();
}

Its based on lazy evaluation where the commands are stored in ArrayLists, and are not evaluated until show or savefig is called.

Recently added imshow and currently working on 3D ploting and animation(though no going well).

Hope someone finds it interesting, here is the repo link https://github.com/halfdisposal/gnuplot-zig.

Supported Zig versions

Tested on the latest zig version zig-0.18.0-dev.146+35accc06e

AI / LLM usage disclosure

The main idea and execution including most of things are done by me myself. AI is used for writings the tests (too lazy to do them manually) and sometimes to figure out random errors I can’t understand and where repitative code is required.

first post btw

1 Like