plotters-rs / plotters

A rust drawing library for high quality data plotting for both WASM and native, statically and realtimely 🦀 📈🚀
https://plotters-rs.github.io/home/
MIT License
3.7k stars 270 forks source link

Option to use cubic spline interpolation instead of linear splines #170

Open richardlett opened 3 years ago

richardlett commented 3 years ago

What is the feature ?

It appears that interpolating between points currently uses linear splines. Using free cubic splines could give smoother looking plots.

(Optional) Why this feature is useful and how people would use the feature ?

I think they look nicer. Free splines often more representative for functions which are continuous. If I remember, correctly, if X is the max of the second derivative on the interval, then linear splines have max error |X|h^2/8, where h is the distance between points.

I'm not sure how they arrived at it, but this document says cubic splines has the error |X| 5/384h^4, where X is the max of the fourth derivative on the interval

Here's a comparison of linear vs cubic for a sine function.

image

I would be happy to take this issue.

38 commented 3 years ago

Hi there, thanks for the suggestion. This is planned for a long time, and I am going to visit it soon, hopefully we will get it with the next major release. - Cheers

xemwebe commented 1 year ago

If someone starts working on this, I would suggest to include Constraint Cubic Splines, too, since they provide curves which look similar smooth (though mathematically they aren't, since they are not everywhere differentiable), but avoid the overshooting problem of Standard Cubic Splines, see e.g. here: constraint cubic spline

GyulyVGC commented 5 months ago

I'm really interested in this feature as well.

GyulyVGC commented 5 months ago

@AaronErhardt is it acceptable to implement this feature using a dependency like this one?

In case it is, I can give it a try.

AaronErhardt commented 5 months ago

@AaronErhardt is it acceptable to implement this feature using a dependency like this one?

Yes, but it's probably better to add a feature flag then so that the spline dependency is only compiled when needed.

GyulyVGC commented 5 months ago

Yes, but it's probably better to add a feature flag then so that the spline dependency is only compiled when needed.

Yeah, that's what I was thinking about as well. I'll give it a try when I have some available time then.

GyulyVGC commented 5 months ago

Update: for my use case, I've solved by just building the spline myself and passing a sampled spline as vector of points to plotters. This way I can accurately choose where to sample it and with which frequency, the interpolation type and so on. So in the end, I think there is actually no strict need to include this feature in plotters itself.

Houski commented 2 months ago

I would definitely use this feature if implemented. :D