Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed the expected type for HeatMap.z from Option<Vec<Z>> to Option<Vec<Vec<Z>>> #157

Merged
merged 8 commits into from
Jun 20, 2024
52 changes: 45 additions & 7 deletions examples/scientific_charts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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::<Vec<f64>>();
let y = (0..100).map(|y| y as f64).collect::<Vec<f64>>();
let z: Vec<Vec<f64>> = x
.iter()
.map(|x| {
y.iter()
.map(|y| (x / 5.0).powf(2.0) + (y / 5.0).powf(2.0))
.collect::<Vec<f64>>()
})
.collect::<Vec<Vec<f64>>>();

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();
}
78 changes: 41 additions & 37 deletions plotly/src/traces/heat_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ where
zauto: Option<bool>,
#[serde(rename = "zhoverformat")]
zhover_format: Option<String>,
zmax: Option<Z>,
zmid: Option<Z>,
zmin: Option<Z>,
zmax: Option<f64>,
zmid: Option<f64>,
zmin: Option<f64>,
zsmooth: Option<Smoothing>,
}

Expand Down Expand Up @@ -179,48 +179,52 @@ 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);
}

#[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",
Expand Down Expand Up @@ -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,
Expand Down
Loading