Skip to content

Commit 4ddb4e1

Browse files
committed
Introduce the convenience function figure::subplots()
1 parent 4133209 commit 4ddb4e1

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

examples/plot_types.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// Inspired from
22
/// https://matplotlib.org/stable/plot_types/index.html#plot-types
3-
use matplotlib::{colors::Base, self as mpl, figure::Figure};
3+
use matplotlib::{colors::Base, self as mpl, figure};
44
use ndarray::Array1;
55
use rand::RngExt;
66

@@ -25,8 +25,7 @@ fn plot_xy() -> anyhow::Result<()> {
2525
let x2: Vec<_> = (0..25).map(|i| 10. / 25. * i as f64).collect();
2626
let y2 = x2.iter().cloned().map(f);
2727

28-
let fig = Figure::new()?;
29-
let [[mut ax]] = fig.subplots()?;
28+
let (fig, [[mut ax]]) = figure::subplots()?;
3029

3130
let y2_above: Vec<_> = y2.clone().map(|y| y + 2.5).collect();
3231
ax.xy(&x2, &y2_above).fmt("x").markeredgewidth(2.).plot();
@@ -55,8 +54,7 @@ fn scatter_xy() -> anyhow::Result<()> {
5554
let sizes = vec(|| rng.random_range(15. .. 80.));
5655
let colors = vec(|| rng.random_range(15 .. 80));
5756

58-
let fig = Figure::new()?;
59-
let [[mut ax]] = fig.subplots()?;
57+
let (fig, [[mut ax]]) = figure::subplots()?;
6058

6159
ax.scatter(&x, &y).s(&sizes).cm(&colors).vmin(0.).vmax(100.).plot();
6260

@@ -70,8 +68,7 @@ fn bar() -> anyhow::Result<()> {
7068
let x: Vec<_> = (0 .. 8).map(|x| 0.5 + x as f64).collect();
7169
let y = [4.8, 5.5, 3.5, 4.6, 6.5, 6.6, 2.6, 3.0];
7270

73-
let fig = Figure::new()?;
74-
let [[mut ax]] = fig.subplots()?;
71+
let (fig, [[mut ax]]) = figure::subplots()?;
7572

7673
ax.bar(&x, &y).width(1.).edgecolor(Base::W).linewidth(0.7).plot();
7774

@@ -85,8 +82,7 @@ fn stem() -> anyhow::Result<()> {
8582
let x = Array1::range(0.5, 8.5, 1.);
8683
let y = [4.8, 5.5, 3.5, 4.6, 6.5, 6.6, 2.6, 3.0];
8784

88-
let fig = Figure::new()?;
89-
let [[mut ax]] = fig.subplots()?;
85+
let (fig, [[mut ax]]) = figure::subplots()?;
9086

9187
ax.stem(&x, &y).plot();
9288

@@ -102,8 +98,7 @@ fn fill_between() -> anyhow::Result<()> {
10298
let y1 = x.map(|&x| 3. + 4. * x / 8. + rng.random_range(0. .. 0.5));
10399
let y2 = x.map(|&x| 1. + 2. * x / 8. + rng.random_range(0. .. 0.5));
104100

105-
let fig = Figure::new()?;
106-
let [[mut ax]] = fig.subplots()?;
101+
let (fig, [[mut ax]]) = figure::subplots()?;
107102

108103
ax.fill_between(&x, &y1, &y2).alpha(0.5).plot();
109104
ax.xy(&x, &((y1 + y2) / 2.)).plot();

src/figure.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ pub struct Figure {
1818
pub(crate) fig: Py<PyAny>, // instance of matplotlib.figure.Figure
1919
}
2020

21+
/// Convenience function returning a [`Figure`] and subplots (the
22+
/// number of which is determined by the size of the array).
23+
pub fn subplots<const R: usize, const C: usize>(
24+
) -> Result<(Figure, [[Axes; C]; R]), Error> {
25+
let fig = Figure::new()?;
26+
let sp = fig.subplots()?;
27+
Ok((fig, sp))
28+
}
29+
2130
#[inline(always)]
2231
fn grid<const R: usize, const C: usize, U>(f: impl Fn(usize, usize) -> U) -> [[U; C]; R] {
2332
let mut r = 0;

0 commit comments

Comments
 (0)