Skip to content

Commit 89a9c6b

Browse files
committed
Add a basic version of stack plots
1 parent 4ddb4e1 commit 89a9c6b

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

examples/plot_types.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn main() -> anyhow::Result<()> {
1616
bar()?;
1717
stem()?;
1818
fill_between()?;
19+
stackplot()?;
1920
Ok(())
2021
}
2122

@@ -108,3 +109,22 @@ fn fill_between() -> anyhow::Result<()> {
108109
fig.save().to_file(format!("{BASE}fill_between.pdf"))?;
109110
Ok(())
110111
}
112+
113+
fn stackplot() -> anyhow::Result<()> {
114+
use ndarray::{arr1, Axis, stack};
115+
116+
let x = Array1::range(0., 10., 2.);
117+
let ay = arr1(&[1., 1.25, 2., 2.75, 3.]);
118+
let by = arr1(&[1., 1., 1., 1., 1.]);
119+
let cy = arr1(&[2., 1., 2., 1., 2.]);
120+
let y = stack(Axis(0), &[ay.view(), by.view(), cy.view()]).unwrap();
121+
122+
let (fig, [[mut ax]]) = figure::subplots()?;
123+
124+
ax.stack(&x, &y).plot();
125+
126+
ax.set_xlim(0., 8.) .set_xticks((1..8).map(f64::from));
127+
ax.set_ylim(0., 8.) .set_yticks((1..8).map(f64::from));
128+
fig.save().to_file(format!("{BASE}stackplot.pdf"))?;
129+
Ok(())
130+
}

src/axes.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,14 @@ impl Axes {
282282
FillBetween::new(self, x, y1, y2)
283283
}
284284

285+
pub fn stack<'a>(
286+
&'a mut self,
287+
x: &'a impl Vector<f64>,
288+
y: &'a ndarray::Array2<f64>,
289+
) -> Stack<'a> {
290+
Stack::new(self, x, y)
291+
}
292+
285293
/// Set the title to `txt` for the Axes.
286294
pub fn set_title(&mut self, txt: impl AsRef<str>) -> &mut Self {
287295
meth!(self.ax, set_title, (txt.as_ref(),)).unwrap();
@@ -1204,6 +1212,35 @@ impl<'a> FillBetween<'a> {
12041212
}
12051213
}
12061214

1215+
pub struct Stack<'a> {
1216+
axes: &'a Axes,
1217+
x: &'a dyn Vector<f64>,
1218+
y: &'a Array2<f64>,
1219+
// FIXME: there are optional arguments.
1220+
}
1221+
1222+
impl<'a> Stack<'a> {
1223+
fn new(
1224+
axes: &'a Axes,
1225+
x: &'a impl Vector<f64>,
1226+
y: &'a Array2<f64>,
1227+
) -> Self {
1228+
Self { axes, x, y }
1229+
}
1230+
1231+
pub fn plot(self) {
1232+
Python::attach(|py| {
1233+
let x = self.x.to_pyvector(py);
1234+
let y = self.y.to_pyarray(py);
1235+
self.axes.ax.bind(py)
1236+
.call_method(intern!(py, "stackplot"),
1237+
(x, y),
1238+
None)
1239+
.unwrap();
1240+
})
1241+
}
1242+
}
1243+
12071244
pub struct QuadContourSet {
12081245
contours: Py<PyAny>,
12091246
}

0 commit comments

Comments
 (0)