Skip to content

lkorasik/gnukt

Repository files navigation

GnuKt - Gnuplot CLI wrapper for Kotlin

by lkorasik

A fluent interface for working with gnuplot from Kotlin.

Install

  1. Install gnuplot on your system
  2. Add a dependency

Currently, Kotlin 1.9 + JVM 21 is supported. (The artifact is not yet posted in maven central)

<dependency>
    <groupId>com.lkorasik.gnukt</groupId>
    <artifactId>core</artifactId>
    <version>0.1</version>
</dependency>

Examples of using

Simple example

Скрипт:

val configuration = GnuktConfiguration("gnuplot")
val script = gnuktScript {
    title("sin \\& cos") // Plot title
    val sin = function { // Declare function
        name("f")
        arguments("x")
        expression("sin(x)")
    }
    val cos = function {
        name("g")
        arguments("x")
        expression("cos(x)")
    }
    xLabel("x") // Set x-axis label
    yLabel("y") // Set y-axis label
    xRange(-5.0, 5.0) // Set x-axis range
    plotFunction(sin, cos) // Plot functions
    pause() // Wait for close
}
Gnukt(configuration).run(script)

Equivalent script:

gnuktScript {     
    title("sin \\& cos")
    val sin = function {
        name("f")
        arguments("x")
        expression("sin(x)")
    }
    val cos = function {
        name("g")
        arguments("x")
        expression("cos(x)")
    }
    xLabel("x")
    yLabel("y")
    xRange(-5.0, 5.0)
    plotFunction(sin, cos)     
    pause()
}.execute(gnuplotPath = "gnuplot")

Result: img.png

How-to

How to export a plot to svg?

You can also replace svg with any other terminal that is supported by gnuplot.

gnuktScript {
    // ...
    val terminal = terminal {
        terminal("svg")
        size {
            width(400)
            height(400)
        }
    }
    terminal(terminal)
    output("out.svg")
    // ...
}

How to draw data from a file?

gnuktScript {
    // ...
    val file1 = file {
        file("example.dat")
        xColumn(1)
        yColumn(3)
    }
    plotFile(file1)
    // ...
}

How to export a script?

gnuktScript {
    // ...
}.save("script.gnuplot")

How to use gnuplot functionality if there is no such command?

gnuktScript {
    // ...
    rawCommand("set key left box")
    // ...
}

What to do if the script is executed, but the window with the graph does

not appear?

Perhaps you forgot to add pause() at the end.

gnuktScript {
    // ...
    pause()
}

Get involved

We welcome contributions. Please check the issue tracker. If you see something you wish to work on, please either comment on the issue, or just send a pull request. Want to work on something else, then just open a issue, and we can discuss! We appreciate documentation improvements, code cleanup, or new features. Please be mindful that all work is done on a volunteer basis, thus we can be slow to reply.