From 1a652cd791560832eaf85fd6b808819c1ed4c075 Mon Sep 17 00:00:00 2001 From: Tectin Date: Mon, 4 Sep 2023 17:29:08 +0200 Subject: [PATCH 1/4] changed the HeatMap z from Vec to Vec> + added `customized_heat_map` function to showcase the working zmin and zmax --- examples/scientific_charts/src/main.rs | 50 ++++++++++++++--- plotly/src/traces/heat_map.rs | 74 ++++++++++++++------------ 2 files changed, 83 insertions(+), 41 deletions(-) diff --git a/examples/scientific_charts/src/main.rs b/examples/scientific_charts/src/main.rs index 20d98a94..c0adb2cf 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}; @@ -109,15 +109,53 @@ fn basic_heat_map() { 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..79e2e715 100644 --- a/plotly/src/traces/heat_map.rs +++ b/plotly/src/traces/heat_map.rs @@ -106,7 +106,7 @@ where y_axis: Option, #[serde(rename = "ycalendar")] y_calendar: Option, - z: Option>, + z: Option>>, zauto: Option, #[serde(rename = "zhoverformat")] zhover_format: Option, @@ -120,7 +120,7 @@ impl HeatMap where Z: Serialize + Clone, { - pub fn new_z(z: Vec) -> Box { + pub fn new_z(z: Vec>) -> Box { Box::new(Self { z: Some(z), ..Default::default() @@ -134,7 +134,7 @@ where Y: Serialize + Clone, Z: Serialize + Clone, { - pub fn new(x: Vec, y: Vec, z: Vec) -> Box { + pub fn new(x: Vec, y: Vec, z: Vec>) -> Box { Box::new(Self { x: Some(x), y: Some(y), @@ -179,7 +179,7 @@ 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], @@ -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", From fdbaac02ccd36b4b131f5de33c6232c2f0a99c82 Mon Sep 17 00:00:00 2001 From: Tectin Date: Mon, 4 Sep 2023 17:31:32 +0200 Subject: [PATCH 2/4] commented `customized_heat_map()` --- examples/scientific_charts/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/scientific_charts/src/main.rs b/examples/scientific_charts/src/main.rs index c0adb2cf..e94ca5b7 100644 --- a/examples/scientific_charts/src/main.rs +++ b/examples/scientific_charts/src/main.rs @@ -157,5 +157,5 @@ fn main() { // Heatmaps // basic_heat_map(); - customized_heat_map(); + // customized_heat_map(); } From 17c0aba13aafdea8a82e44820b463d9a217431cc Mon Sep 17 00:00:00 2001 From: Tectin Date: Tue, 5 Sep 2023 17:40:49 +0200 Subject: [PATCH 3/4] updated tests and example --- examples/custom_controls/src/main.rs | 2 +- plotly/src/traces/heat_map.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/custom_controls/src/main.rs b/examples/custom_controls/src/main.rs index 2f285361..46b1aeb5 100644 --- a/examples/custom_controls/src/main.rs +++ b/examples/custom_controls/src/main.rs @@ -44,7 +44,7 @@ fn bar_plot_with_dropdown_for_different_data() { /// Display a heat map, with buttons to allow for toggling of different /// colorscales. fn heat_map_with_modifiable_colorscale() { - type HeatMapType = HeatMap>; + type HeatMapType = HeatMap; let gauss = |v: i32| (-v as f64 * v as f64 / 200.0).exp(); let z = (-30..30) .map(|x| (-30..30).map(|y| gauss(x) * gauss(y)).collect_vec()) diff --git a/plotly/src/traces/heat_map.rs b/plotly/src/traces/heat_map.rs index 79e2e715..bf8c0fbf 100644 --- a/plotly/src/traces/heat_map.rs +++ b/plotly/src/traces/heat_map.rs @@ -182,7 +182,7 @@ mod tests { 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); @@ -253,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, From 4c8b3bc08730099abd82152c692798aabefae0ad Mon Sep 17 00:00:00 2001 From: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> Date: Wed, 19 Jun 2024 14:23:02 +0200 Subject: [PATCH 4/4] hardcode HeatMap zmin/zmax/zmid type to f64 Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com> --- examples/custom_controls/src/main.rs | 2 +- examples/scientific_charts/src/main.rs | 2 +- plotly/src/traces/heat_map.rs | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/custom_controls/src/main.rs b/examples/custom_controls/src/main.rs index 46b1aeb5..2f285361 100644 --- a/examples/custom_controls/src/main.rs +++ b/examples/custom_controls/src/main.rs @@ -44,7 +44,7 @@ fn bar_plot_with_dropdown_for_different_data() { /// Display a heat map, with buttons to allow for toggling of different /// colorscales. fn heat_map_with_modifiable_colorscale() { - type HeatMapType = HeatMap; + type HeatMapType = HeatMap>; let gauss = |v: i32| (-v as f64 * v as f64 / 200.0).exp(); let z = (-30..30) .map(|x| (-30..30).map(|y| gauss(x) * gauss(y)).collect_vec()) diff --git a/examples/scientific_charts/src/main.rs b/examples/scientific_charts/src/main.rs index e94ca5b7..34ace161 100644 --- a/examples/scientific_charts/src/main.rs +++ b/examples/scientific_charts/src/main.rs @@ -102,7 +102,7 @@ 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); diff --git a/plotly/src/traces/heat_map.rs b/plotly/src/traces/heat_map.rs index bf8c0fbf..07c216ed 100644 --- a/plotly/src/traces/heat_map.rs +++ b/plotly/src/traces/heat_map.rs @@ -106,13 +106,13 @@ where y_axis: Option, #[serde(rename = "ycalendar")] y_calendar: Option, - z: Option>>, + z: Option>, zauto: Option, #[serde(rename = "zhoverformat")] zhover_format: Option, - zmax: Option, - zmid: Option, - zmin: Option, + zmax: Option, + zmid: Option, + zmin: Option, zsmooth: Option, } @@ -120,7 +120,7 @@ impl HeatMap where Z: Serialize + Clone, { - pub fn new_z(z: Vec>) -> Box { + pub fn new_z(z: Vec) -> Box { Box::new(Self { z: Some(z), ..Default::default() @@ -134,7 +134,7 @@ where Y: Serialize + Clone, Z: Serialize + Clone, { - pub fn new(x: Vec, y: Vec, z: Vec>) -> Box { + pub fn new(x: Vec, y: Vec, z: Vec) -> Box { Box::new(Self { x: Some(x), y: Some(y),