#plotters #layout #utility #chart #title #margin #chart-layout

plotters-layout

Layout utility for plotters crate

1 unstable release

0.1.0 Feb 1, 2023

#396 in Visualization

MIT license

16KB
303 lines

plotters-layout

This is a layout utility library for plotters crate.


lib.rs:

plotters-layout

Layout utility library for plotters crate.

Creating a chart whose plotting area has specified size

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));

Adjusting aspect ratio

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));

Dependencies

~14MB
~104K SLoC