Skip to content

Commit a6302ea

Browse files
tectin0andrei-ng
andauthored
changed the expected type for HeatMap.z from Option<Vec<Z>> to Option<Vec<Vec<Z>>> (#157)
* changed the HeatMap `zmin, zmax` and `zmid` setters to take `f64` arguments - with `Vec<Vec<T>>` as an input for `HeatMap.z` it infers that `Z` has the type `Vec<T>` which breaks the setter for `zmin`, `zmax` and `zmid` . The take scalar types and should be inferred as `T` * added `customized_heat_map` function to showcase the working `zmin` and `zmax` * updated tests and example Signed-off-by: Andrei Gherghescu <[email protected]> --------- Signed-off-by: Andrei Gherghescu <[email protected]> Co-authored-by: Andrei Gherghescu <[email protected]>
1 parent 0071831 commit a6302ea

File tree

2 files changed

+86
-44
lines changed

2 files changed

+86
-44
lines changed

examples/scientific_charts/src/main.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::f64::consts::PI;
44

5-
use plotly::common::{ColorScale, ColorScalePalette, Title};
5+
use plotly::common::{ColorScale, ColorScalePalette, Font, Title};
66
use plotly::contour::Contours;
77
use plotly::{Contour, HeatMap, Layout, Plot};
88

@@ -102,22 +102,60 @@ fn customizing_spacing_between_x_and_y_ticks() {
102102
// Heatmaps
103103
fn basic_heat_map() {
104104
let z = vec![vec![1, 20, 30], vec![20, 1, 60], vec![30, 60, 1]];
105-
let trace = HeatMap::new_z(z);
105+
let trace = HeatMap::new_z(z).zmin(1.0).zmax(60.0);
106106
let mut plot = Plot::new();
107107
plot.add_trace(trace);
108108

109109
plot.show();
110110
}
111111

112+
fn customized_heat_map() {
113+
let x = (0..100).map(|x| x as f64).collect::<Vec<f64>>();
114+
let y = (0..100).map(|y| y as f64).collect::<Vec<f64>>();
115+
let z: Vec<Vec<f64>> = x
116+
.iter()
117+
.map(|x| {
118+
y.iter()
119+
.map(|y| (x / 5.0).powf(2.0) + (y / 5.0).powf(2.0))
120+
.collect::<Vec<f64>>()
121+
})
122+
.collect::<Vec<Vec<f64>>>();
123+
124+
let (_z_min, z_max) = z
125+
.iter()
126+
.flatten()
127+
.fold((f64::MAX, f64::MIN), |(min, max), &x| {
128+
(min.min(x), max.max(x))
129+
});
130+
131+
let colorscale = ColorScalePalette::Jet;
132+
133+
let trace = HeatMap::new(x, y, z)
134+
.zmin(z_max * 0.4)
135+
.zmax(z_max * 0.5)
136+
.color_scale(colorscale.into());
137+
138+
let layout = Layout::new()
139+
.title(Title::new("Customized Heatmap"))
140+
.font(Font::new().size(32));
141+
142+
let mut plot = Plot::new();
143+
plot.set_layout(layout);
144+
plot.add_trace(trace);
145+
146+
plot.show();
147+
}
148+
112149
fn main() {
113150
// Uncomment any of these lines to display the example.
114151

115152
// Contour Plots
116-
simple_contour_plot();
117-
colorscale_for_contour_plot();
118-
customizing_size_and_range_of_a_contour_plots_contours();
119-
customizing_spacing_between_x_and_y_ticks();
153+
// simple_contour_plot();
154+
// colorscale_for_contour_plot();
155+
// customizing_size_and_range_of_a_contour_plots_contours();
156+
// customizing_spacing_between_x_and_y_ticks();
120157

121158
// Heatmaps
122-
basic_heat_map();
159+
// basic_heat_map();
160+
// customized_heat_map();
123161
}

plotly/src/traces/heat_map.rs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ where
110110
zauto: Option<bool>,
111111
#[serde(rename = "zhoverformat")]
112112
zhover_format: Option<String>,
113-
zmax: Option<Z>,
114-
zmid: Option<Z>,
115-
zmin: Option<Z>,
113+
zmax: Option<f64>,
114+
zmid: Option<f64>,
115+
zmin: Option<f64>,
116116
zsmooth: Option<Smoothing>,
117117
}
118118

@@ -179,48 +179,52 @@ mod tests {
179179

180180
#[test]
181181
fn test_serialize_heat_map_z() {
182-
let trace = HeatMap::new_z(vec![1.0]);
182+
let trace = HeatMap::new_z(vec![vec![1.0]]);
183183
let expected = json!({
184184
"type": "heatmap",
185-
"z": [1.0],
185+
"z": [[1.0]],
186186
});
187187

188188
assert_eq!(to_value(trace).unwrap(), expected);
189189
}
190190

191191
#[test]
192192
fn test_serialize_heat_map() {
193-
let trace = HeatMap::new(vec![0.0, 1.0], vec![2.0, 3.0], vec![4.0, 5.0])
194-
.auto_color_scale(true)
195-
.color_bar(ColorBar::new())
196-
.color_scale(ColorScale::Palette(ColorScalePalette::Picnic))
197-
.connect_gaps(false)
198-
.hover_info(HoverInfo::None)
199-
.hover_label(Label::new())
200-
.hover_on_gaps(true)
201-
.hover_template("tmpl")
202-
.hover_template_array(vec!["tmpl1", "tmpl2"])
203-
.hover_text(vec!["hov", "er"])
204-
.legend_group("1")
205-
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
206-
.name("name")
207-
.opacity(0.99)
208-
.reverse_scale(false)
209-
.show_legend(true)
210-
.show_scale(false)
211-
.text(vec!["te", "xt"])
212-
.transpose(true)
213-
.visible(Visible::LegendOnly)
214-
.x_axis("x")
215-
.x_calendar(Calendar::Hebrew)
216-
.y_axis("y")
217-
.y_calendar(Calendar::Islamic)
218-
.zauto(true)
219-
.zhover_format("fmt")
220-
.zmax(10.0)
221-
.zmid(5.0)
222-
.zmin(0.0)
223-
.zsmooth(Smoothing::Fast);
193+
let trace = HeatMap::new(
194+
vec![0.0, 1.0],
195+
vec![2.0, 3.0],
196+
vec![vec![4.0, 5.0], vec![6.0, 7.0]],
197+
)
198+
.auto_color_scale(true)
199+
.color_bar(ColorBar::new())
200+
.color_scale(ColorScale::Palette(ColorScalePalette::Picnic))
201+
.connect_gaps(false)
202+
.hover_info(HoverInfo::None)
203+
.hover_label(Label::new())
204+
.hover_on_gaps(true)
205+
.hover_template("tmpl")
206+
.hover_template_array(vec!["tmpl1", "tmpl2"])
207+
.hover_text(vec!["hov", "er"])
208+
.legend_group("1")
209+
.legend_group_title(LegendGroupTitle::new("Legend Group Title"))
210+
.name("name")
211+
.opacity(0.99)
212+
.reverse_scale(false)
213+
.show_legend(true)
214+
.show_scale(false)
215+
.text(vec!["te", "xt"])
216+
.transpose(true)
217+
.visible(Visible::LegendOnly)
218+
.x_axis("x")
219+
.x_calendar(Calendar::Hebrew)
220+
.y_axis("y")
221+
.y_calendar(Calendar::Islamic)
222+
.zauto(true)
223+
.zhover_format("fmt")
224+
.zmax(10.0)
225+
.zmid(5.0)
226+
.zmin(0.0)
227+
.zsmooth(Smoothing::Fast);
224228

225229
let expected = json!({
226230
"type": "heatmap",
@@ -249,7 +253,7 @@ mod tests {
249253
"y": [2.0, 3.0],
250254
"yaxis": "y",
251255
"ycalendar": "islamic",
252-
"z": [4.0, 5.0],
256+
"z": [[4.0, 5.0], [6.0, 7.0]],
253257
"zauto": true,
254258
"zhoverformat": "fmt",
255259
"zmax": 10.0,

0 commit comments

Comments
 (0)