Skip to content
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

68 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graplot

Crates.io version Docs

'graplot' is an experimental plotting library written in Rust that is based on macroquad (internally litequad). It creates a window displaying the graphs.

Installation

Add 'graplot' as a dependency:

[dependencies]
graplot = "0.1.9"
use graplot::Plot;

let plot = Plot::new([-4., -2., 1., 4.]);
plot.show();

plot1

Multiple graphs:

use graplot::Plot;

let xs = [1., 2., 3.,];
let ys = [1.7, 3., 1.9];

let ys1 = [1.4, 1.6, 1.5];    

let ys2 = [0.9, 1.2, 1.7, 1.9, 2.];    

let mut plot = Plot::new((xs, ys));
plot.add((xs, ys1, "c-o"));
plot.add((ys2, "r-"));
plot.show();

multiple graphs

Label the x and y axis and set a title:

use graplot::{x, Plot};

let mut plot = Plot::new((|x: f64| x.cos(), x(6.)));

plot.set_title("cosine wave");

plot.set_xlabel("x axis");
plot.set_ylabel("y axis");
plot.show();

cosine labeled

Sine wave:

use graplot::Plot;

let mut xs = [0.; 1000]; 

let mut add = 0f64;
for idx in 0..1000 {
    xs[idx] = add/1000.;
    add += 1.;
}
    
let mut ys = [0.; 1000];
for (i, y) in ys.iter_mut().enumerate() {
    *y = (2. * std::f64::consts::PI * xs[i]).sin();
}
// or alternatively: let plot = Plot::new((|x: f64| x.sin(), x(4.)));
let plot = Plot::new((xs, ys));
plot.show();

Sinewave

x³ + x² - 0.08:

use graplot::{Plot, x};

// x(...) ... sets the absolute max value for x 
let plot = Plot::new((|x: f64| x.powf(3.) + x.powf(2.) - 0.08, x(1.)) );
plot.show();

pol3

x² - 0.5:

use graplot::Plot;

let plot = Plot::new(|x: f64| x.powf(2.) - 0.5);
plot.show();

squared

Using a line description: (matplotlib)

use graplot::Plot;

// c ... cyan color, - ... solid line, o ... ring marker
let plot = Plot::new(([-4., -3., -3.4, -3.75, -4.1], "c-o"));
plot.show();

line_desc

Custom scaling:

use graplot::{Desc, Plot, x};

let mut plot = Plot::new((|x: f64| x.cos(), x(2.)));
plot.set_desc(Desc {
    min_steps_x: 6.,
    spacing_x: 47.,
    ..Default::default()
});
plot.show();

Another version for miniquad is needed for this feature. Therefore is it broken. Spawning multiple windows on linux:

let mut plot = Plot::new(|x: f64| x.powf(3.) + x.powf(2.) - 0.08);
plot.set_title("x^3 + x^2 - 0.08");
let h = plot.show_threaded() // show_threaded() is currently linux only;

let mut plot = Plot::new(|x: f64| x.powf(2.) + 0.08);
plot.set_title("x²");
plot.show();

h.join().unwrap() // you need to close both windows

multiple windows

Changelog

  • 0.1.9: fixed bug
  • 0.1.8: set color now uses 3 args, fixed step size
  • 0.1.7: Set graph color, custom x & y "line" spacing and step size | "custom scaling"
  • 0.1.6: Label x axis, litequad
  • 0.1.5: y axis, set title, /mutliple windows on linux/ | yanked
  • 0.1.4: Multiple graphs

About

Plotting library written in Rust

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages