diff --git a/examples/scientific_charts/src/main.rs b/examples/scientific_charts/src/main.rs index 20d98a94..34ace161 100644 --- a/examples/scientific_charts/src/main.rs +++ b/examples/scientific_charts/src/main.rs @@ -2,7 +2,7 @@ use std::f64::consts::PI; -use plotly::common::{ColorScale, ColorScalePalette, Title}; +use plotly::common::{ColorScale, ColorScalePalette, Font, Title}; use plotly::contour::Contours; use plotly::{Contour, HeatMap, Layout, Plot}; @@ -102,22 +102,60 @@ fn customizing_spacing_between_x_and_y_ticks() { // Heatmaps fn basic_heat_map() { let z = vec![vec![1, 20, 30], vec![20, 1, 60], vec![30, 60, 1]]; - let trace = HeatMap::new_z(z); + let trace = HeatMap::new_z(z).zmin(1.0).zmax(60.0); let mut plot = Plot::new(); plot.add_trace(trace); plot.show(); } +fn customized_heat_map() { + let x = (0..100).map(|x| x as f64).collect::>(); + let y = (0..100).map(|y| y as f64).collect::>(); + let z: Vec> = x + .iter() + .map(|x| { + y.iter() + .map(|y| (x / 5.0).powf(2.0) + (y / 5.0).powf(2.0)) + .collect::>() + }) + .collect::>>(); + + let (_z_min, z_max) = z + .iter() + .flatten() + .fold((f64::MAX, f64::MIN), |(min, max), &x| { + (min.min(x), max.max(x)) + }); + + let colorscale = ColorScalePalette::Jet; + + let trace = HeatMap::new(x, y, z) + .zmin(z_max * 0.4) + .zmax(z_max * 0.5) + .color_scale(colorscale.into()); + + let layout = Layout::new() + .title(Title::new("Customized Heatmap")) + .font(Font::new().size(32)); + + let mut plot = Plot::new(); + plot.set_layout(layout); + plot.add_trace(trace); + + plot.show(); +} + fn main() { // Uncomment any of these lines to display the example. // Contour Plots - simple_contour_plot(); - colorscale_for_contour_plot(); - customizing_size_and_range_of_a_contour_plots_contours(); - customizing_spacing_between_x_and_y_ticks(); + // simple_contour_plot(); + // colorscale_for_contour_plot(); + // customizing_size_and_range_of_a_contour_plots_contours(); + // customizing_spacing_between_x_and_y_ticks(); // Heatmaps - basic_heat_map(); + // basic_heat_map(); + // customized_heat_map(); } diff --git a/plotly/src/traces/heat_map.rs b/plotly/src/traces/heat_map.rs index 151b6941..07c216ed 100644 --- a/plotly/src/traces/heat_map.rs +++ b/plotly/src/traces/heat_map.rs @@ -110,9 +110,9 @@ where zauto: Option, #[serde(rename = "zhoverformat")] zhover_format: Option, - zmax: Option, - zmid: Option, - zmin: Option, + zmax: Option, + zmid: Option, + zmin: Option, zsmooth: Option, } @@ -179,10 +179,10 @@ mod tests { #[test] fn test_serialize_heat_map_z() { - let trace = HeatMap::new_z(vec![1.0]); + let trace = HeatMap::new_z(vec![vec![1.0]]); let expected = json!({ "type": "heatmap", - "z": [1.0], + "z": [[1.0]], }); assert_eq!(to_value(trace).unwrap(), expected); @@ -190,37 +190,41 @@ mod tests { #[test] fn test_serialize_heat_map() { - let trace = HeatMap::new(vec![0.0, 1.0], vec![2.0, 3.0], vec![4.0, 5.0]) - .auto_color_scale(true) - .color_bar(ColorBar::new()) - .color_scale(ColorScale::Palette(ColorScalePalette::Picnic)) - .connect_gaps(false) - .hover_info(HoverInfo::None) - .hover_label(Label::new()) - .hover_on_gaps(true) - .hover_template("tmpl") - .hover_template_array(vec!["tmpl1", "tmpl2"]) - .hover_text(vec!["hov", "er"]) - .legend_group("1") - .legend_group_title(LegendGroupTitle::new("Legend Group Title")) - .name("name") - .opacity(0.99) - .reverse_scale(false) - .show_legend(true) - .show_scale(false) - .text(vec!["te", "xt"]) - .transpose(true) - .visible(Visible::LegendOnly) - .x_axis("x") - .x_calendar(Calendar::Hebrew) - .y_axis("y") - .y_calendar(Calendar::Islamic) - .zauto(true) - .zhover_format("fmt") - .zmax(10.0) - .zmid(5.0) - .zmin(0.0) - .zsmooth(Smoothing::Fast); + let trace = HeatMap::new( + vec![0.0, 1.0], + vec![2.0, 3.0], + vec![vec![4.0, 5.0], vec![6.0, 7.0]], + ) + .auto_color_scale(true) + .color_bar(ColorBar::new()) + .color_scale(ColorScale::Palette(ColorScalePalette::Picnic)) + .connect_gaps(false) + .hover_info(HoverInfo::None) + .hover_label(Label::new()) + .hover_on_gaps(true) + .hover_template("tmpl") + .hover_template_array(vec!["tmpl1", "tmpl2"]) + .hover_text(vec!["hov", "er"]) + .legend_group("1") + .legend_group_title(LegendGroupTitle::new("Legend Group Title")) + .name("name") + .opacity(0.99) + .reverse_scale(false) + .show_legend(true) + .show_scale(false) + .text(vec!["te", "xt"]) + .transpose(true) + .visible(Visible::LegendOnly) + .x_axis("x") + .x_calendar(Calendar::Hebrew) + .y_axis("y") + .y_calendar(Calendar::Islamic) + .zauto(true) + .zhover_format("fmt") + .zmax(10.0) + .zmid(5.0) + .zmin(0.0) + .zsmooth(Smoothing::Fast); let expected = json!({ "type": "heatmap", @@ -249,7 +253,7 @@ mod tests { "y": [2.0, 3.0], "yaxis": "y", "ycalendar": "islamic", - "z": [4.0, 5.0], + "z": [[4.0, 5.0], [6.0, 7.0]], "zauto": true, "zhoverformat": "fmt", "zmax": 10.0,