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

Conversation

tectin0
Copy link
Contributor

@tectin0 tectin0 commented Sep 4, 2023

With Vec<Vec<T>> as an input for HeatMap.z it infers that Z has the type Vec<T> instead of T. This prevents .zmin() and .zmax() from working.

Example:

use plotly::common::{ColorScalePalette, Font, Title};

use plotly::{HeatMap, Layout, Plot};

fn main() {
    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 colorscale = ColorScalePalette::Jet;

    let trace = HeatMap::new(x, y, z)
        .zmin(400.0) // does not work expects a Vec<f64>
        .zmax(600.0) // same here
        .color_scale(colorscale.into());

    let layout = Layout::new().font(Font::new().size(32));

    let mut plot = Plot::new();
    plot.set_layout(layout);
    plot.add_trace(trace);

    plot.show();
}

Changing the type to Option<Vec<Vec<Z>> results in a correctly inferred type of Z and .zmin() and .zmax() work correctly

newplot

also added customized_heat_map function to showcase the working zmin and zmax

added `customized_heat_map` function to showcase the working zmin and zmax
@tectin0
Copy link
Contributor Author

tectin0 commented Sep 4, 2023

Going to look into the failed tests

@tectin0 tectin0 marked this pull request as draft September 4, 2023 16:10
@tectin0 tectin0 marked this pull request as ready for review September 22, 2023 19:21
Signed-off-by: Andrei Gherghescu <8067229+andrei-ng@users.noreply.github.com>
@andrei-ng andrei-ng self-assigned this Jun 12, 2024
@andrei-ng
Copy link
Collaborator

andrei-ng commented Jun 19, 2024

Hi @tectin0. Thank for looking into this issue, for the PR and for the nice example you've added.

I had a bit more time to look into it. As you clearly identified, indeed there is a problem when With Vec<Vec<T>> as an input for HeatMap.z and the inferred Z for zmin, zmax.

However, for this issue I would rather go a different path and force the type for those setters to be f64 and not touch the input type for Z. The genericity of Z is broad enough to make it work with different input types / arrays.

I pushed a commit on top of your PR with that change. @tectin0 do you agree?

Copy link

codecov bot commented Jun 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.56%. Comparing base (4815df7) to head (ba20d96).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #157      +/-   ##
==========================================
- Coverage   93.61%   93.56%   -0.05%     
==========================================
  Files          27       28       +1     
  Lines        7153     7215      +62     
==========================================
+ Hits         6696     6751      +55     
- Misses        457      464       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@andrei-ng
Copy link
Collaborator

As @tectin0 gave me a 👍 , I will merge it.

@andrei-ng andrei-ng merged commit a6302ea into plotly:main Jun 20, 2024
18 of 21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants