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

Animation support like for python #58

Open
Tastaturtaste opened this issue Apr 11, 2022 · 0 comments
Open

Animation support like for python #58

Tastaturtaste opened this issue Apr 11, 2022 · 0 comments

Comments

@Tastaturtaste
Copy link

This is a feature request for supporting a animated plot like seen in this python example.
I would expect it to work by giving for x and y a slice of slices or a slice of Vecs instead of a single slice.
Given this python example:

import plotly.express as px
df = px.data.gapminder()
px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year", animation_group="country",
           size="pop", color="continent", hover_name="country",
           log_x=True, size_max=55, range_x=[100,100000], range_y=[25,90])

A usage example could look like the following code:

use plotly::{common::Mode,Plot,Scatter};
fn main() -> Result<(),BoxErr> {
    // Four different time points
    let time = vec![1.0,2.0,3.0,4.0]; 
    // For each timepoint generate data. This could be a position for example.
    let mut x = Vec::with_capacity(time.len());
    for i in 0..time.len() {
        x.push((0..1_000).map(|x|x as f64*0.1).collect::<Vec<f64>>());
    }
    // For each timepoint evaluate some function at each position
    let mut y = Vec::with_capacity(time.len());
    for (i,xi) in x.iter().enumerate() {
        y.push(
            xi.iter().map(|xk| xk*xk + time[i]).collect::<Vec<f64>>()
        );
    }
    // Generate traces from the data
    let traces = x.into_iter().zip(y).map(|(x,y)| Scatter::new(x,y).mode(Mode::Lines)).collect::<Vec<_>>();
    // Plot the data and animate the plot with respect to time
    let mut plot = Plot::new();
    plot.add_traces(traces).animate_by(time);
    plot.show();
    Ok(())
}
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

No branches or pull requests

1 participant