Skip to content

Rotation of a Shape relative to its Layout #363

Description

@dustypomerleau

Thanks for all the time you've been putting into this crate recently.

I would like to plot a confidence interval on a ScatterPolar plot, using an ellipse:

Image

This looks very similar to this example from the book, but it also requires the abilty to rotate the ellipse relative to the layout.

Is it possible to rotate a shape? Am I missing a more obvious solution?

Activity

  1. andrei-ng commented on Sep 16, 2025

    @andrei-ng
    Collaborator

    @dustypomerleau The main problem is that Plotly.js doesn't actually support a true ellipse shape, see plotly/plotly.js#4296 . The ones in the book are based on the Circle shape, but skewed, it is basically an axis aligned ellipse.

    Since plotly.rs is a wrapper on plotly.js, we inherit the same issue.

    Sadly, the only option you have is to actually generate your own scatter trace with the ellipse equation and add it to your plot. This is what the python and js community has been doing so far. https://community.plotly.com/t/how-to-draw-ellipse-on-top-of-scatter-plot/36576/2

  2. dustypomerleau commented on Sep 17, 2025

    @dustypomerleau
    ContributorAuthor

    Thanks for the pointer, @andrei-ng. I think achieving this on a polar plot might require me to convert the dataset from polar to cartesian coordinates to do the calculations for the ellipse. It could be quite challenging (for me, at least). For the moment, I'll leave this open and post my solution if I find one.

  3. andrei-ng commented on Sep 18, 2025

    @andrei-ng
    Collaborator

    Thanks for the pointer, @andrei-ng. I think achieving this on a polar plot might require me to convert the dataset from polar to cartesian coordinates to do the calculations for the ellipse. It could be quite challenging (for me, at least). For the moment, I'll leave this open and post my solution if I find one.

    Yes, correct. You would have to do it in cartesian first and then project it to polar coordinates and then plot it. Probably doable, but indeed not one single API call :(.

  4. dustypomerleau commented on Sep 26, 2025

    @dustypomerleau
    ContributorAuthor

    Wow, what a journey...

    Image

    As you can see from the image, I have solved this problem for (at a minimum) my use case. After some statistical education, I concluded that I should plot a tolerance interval (a range where most values fall), rather than a confidence interval (a range likely to contain the mean), but the main article I used as guidance still refers to the generated plot as a "confidence ellipse." Suffice it to say that I don't have the statistical expertise to know the perfect terminology.

    You can look at the main function for drawing the ellipse, as well as a plot similar to the image above that makes use of it.

    The main issue is that some transformations are easier in cartesian coordinates and others (rotation) are easier in polar coordinates. Unfortunately, the order of the transformations matters to the technique in the article, so following it requires moving between polar and cartesian coordinates more than once.

    If you are interested, I believe that it should be possible to generalize this solution and upstream it. I haven't yet written a function to draw the ellipse on cartesian plots, but it should not be difficult now that I've done so for polar plots.

    For example, I think we could put this in a trait, and implement it only for Scatter<f64, f64> and ScatterPolar<f64, f64>, and that would cover the majority of use cases. I can't see any way to implement it generically for ScatterPolar<Theta, R>, because the only trait bounds on Theta and R are Serialize + Clone + 'static, so they may not even be numeric.

  5. andrei-ng commented on Oct 8, 2025

    @andrei-ng
    Collaborator

    @dustypomerleau , wow, nicely done! :)

    Thanks for sharing the article and your implementation. I guess, drawing the ellipse on the chartesian should be easier than what you went through.

    If you are willing to upstream your part, even if only as an example in the examples/ folder and in the mdBook, that would already be a great addition IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions