1 个不稳定版本
0.1.0 | 2023年2月1日 |
---|
#423 在 可视化
16KB
303 行
plotters-layout
这是一个为 plotters
crate 提供布局工具的库。
lib.rs
:
plotters-layout
为 plotters crate 提供布局工具库。
创建具有指定绘图区域大小的图表
use plotters::prelude::*;
use plotters_layout::ChartLayout;
use plotters::backend::{RGBPixel, PixelFormat};
let mut layout = ChartLayout::new();
layout.caption("Graph Title", ("sans-serif", 40))?
.margin(4)
.x_label_area_size(40)
.y_label_area_size(40);
let (w, h): (u32, u32) = layout.desired_image_size((200, 160));
let mut buf = vec![0u8; (w * h) as usize * RGBPixel::PIXEL_SIZE];
let graph = BitMapBackend::with_buffer(&mut buf, (w, h));
let root_area = graph.into_drawing_area();
let builder = layout.bind(&root_area)?;
let chart = builder.build_cartesian_2d(0f64..20f64, 0f64..16f64)?;
assert_eq!(chart.plotting_area().dim_in_pixel(), (200, 160));
调整长宽比
use plotters::prelude::*;
use plotters_layout::{centering_ranges, ChartLayout};
let min_range = (-200f64..200f64, -100f64..100f64);
let mut buf = String::new();
let graph = SVGBackend::with_string(&mut buf, (1280, 720));
let root_area = graph.into_drawing_area();
let mut builder = ChartLayout::new()
.caption("Graph Title", ("sans-serif", 40))?
.margin(4)
.x_label_area_size(40)
.y_label_area_size(40)
.bind(&root_area)?;
let (width, height) = builder.estimate_plot_area_size();
let (x_range, y_range) = centering_ranges(&min_range, &(width as f64, height as f64));
// (x_range, y_range) and (width, height) has same aspect ratio
let inner_ratio = (x_range.end - x_range.start) / (y_range.end - y_range.start);
let outer_ratio = width as f64 / height as f64;
assert!((inner_ratio - outer_ratio).abs() < 1e-8);
let chart = builder.build_cartesian_2d(x_range, y_range)?;
assert_eq!(chart.plotting_area().dim_in_pixel(), (width, height));
依赖关系
~14MB
~104K SLoC