-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy path3d-plot2.rs
57 lines (50 loc) · 1.66 KB
/
3d-plot2.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use plotters::prelude::*;
use std::f64::consts::PI;
fn pdf(x: f64, y: f64) -> f64 {
// see https://mathworld.wolfram.com/BivariateNormalDistribution.html
// assumes x_bar = 0 and y_bar = 0
const SDX: f64 = 0.3;
const SDY: f64 = 0.3;
const vx: f64 = SDX * SDX;
const vy : f64 = SDY * SDY;
const sdx_sdy: f64 = SDX * SDY;
const RHO : f64 = 0.9;
const c1: f64 = 1.0 - RHO * RHO;
let c2 : f64 = c1.sqrt();
let A: f64 = 1.0 / (2.0 * PI * sdx_sdy * c2);
let Z: f64 = x * x / vx - 2.0 * RHO * x * y / sdx_sdy + y * y / vy;
let res = A * (-Z / 2.0 / c1).exp();
res
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let root =
BitMapBackend::gif("plotters-doc-data/3d-plot2.gif", (600, 400), 100)?.into_drawing_area();
for pitch in 0..157 {
root.fill(&WHITE)?;
let mut chart = ChartBuilder::on(&root)
.caption("2D Guassian PDF", ("sans-serif", 20))
.build_cartesian_3d(-1.0..1.0, 0.0..3.0, -1.0..1.0)?;
chart.with_projection(|mut p| {
p.pitch = 1.57 - (1.57 - pitch as f64 / 50.0).abs();
p.scale = 0.7;
p.into_matrix() // build the projection matrix
});
chart.configure_axes().draw()?;
chart.draw_series(
SurfaceSeries::xoz(
(-30..=30).map(|x| x as f64 / 30.0),
(-30..=30).map(|x| x as f64 / 30.0),
pdf,
)
.style_func(&|&v| {
(&HSLColor(240.0 / 360.0 - 240.0 / 360.0 * v / 5.0, 1.0, 0.7)).into()
}),
)?;
root.present()?;
}
Ok(())
}
#[test]
fn entry_point() {
main().unwrap()
}