5个不稳定版本

0.3.0 2023年8月10日
0.2.0 2023年6月8日
0.2.0-alpha2023年6月7日
0.1.1 2023年6月5日
0.1.0 2023年6月5日

#206可视化

Download history 16/week @ 2024-04-03 1/week @ 2024-05-22

每月 62 次下载
oscirs 中使用

Apache-2.0

44KB
706

oscirs_plot

crates.io

Rust绘图库

描述

这个库专注于基于浮点向量进行数据绘图。目前它只支持线形图,但在这些线形图中,您可以等比例缩放轴,设置自定义轴限制,添加图例,给绘图加标题,并设置轴标签。

使用方法

为了快速入门,从svgplot_core导入所有内容并初始化一个新图。使用默认构造函数并创建一个 Scatterline 对象。使图变量可变,因为它将在内部动态存储数据系列。

use oscirs_plot::svgplot_core::*;

let mut figure: Scatterline = Scatterline::default();

一旦我们有了图,我们就可以设置轴标签和图标题。

figure.label_x("X axis (unit)");
figure.label_y("Y axis (unit)");
figure.title("This is a plot");

现在我们可以创建一些数据向量并指定绘图系列的样式。我们将使它可变,以便我们可以重复使用相同的样式对象来绘制另一个数据系列。在这个例子中,我选择用蓝色绘制平方根函数。

let x: Vec<f32> = (0..=6)
    .map(|x| x as f32)
    .collect();
let y: Vec<f32> = x.clone()
    .into_iter()
    .map(|x| x.sqrt())
    .collect();

let mut style: PlotStyle = PlotStyle {
    stroke_color: Color::Blue,
    ..Default::default()
};

figure.add_data(&x, &y, &style)
    .expect("Failed to add data series");

我们还可以通过指定描边宽度为0并打开标记来创建散点图。让我们添加一个线 y=x 的散点系列。

let y2: Vec<f32> = x.clone();

style.stroke_color = Color::Red;
style.stroke_width = 0;
style.has_markers = true;

figure.add_data(&x, &y2, &style)
    .expect("Failed to add data series");

要显示我们的图,我们只需在我们的图上调用 render() 并指定一个文件名。.svg 文件将在写作过程结束时自动生成并打开。

figure.render("Example_Figure").expect("Failed to generate figure");

依赖项

~77KB