# \`Plots.jl\` spline

**URL:** https://discourse.julialang.org/t/plots-jl-spline/88720
**Category:** General Usage
**Tags:** question, package, plotting, splines
**Created:** [October 14, 2022, 10:13am UTC](https://discourse.julialang.org/t/plots-jl-spline/88720 "2022-10-14T10:13:41Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [October 14, 2022, 10:13am UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/1 "2022-10-14T10:13:41Z")

</div>

How can I plot a line with `line_style=spline` in Plots.jl? For an example of what I’m after see the ‘spline’ trace in these [plotly docs](https://plotly.com/python/line-charts/#interpolation-with-line-plots)

The closest I can find in the docs is about [how to step](https://docs.juliaplots.org/stable/gallery/pyplot/generated/pyplot-ref53/)

Any suggestions should be able to make this plot smooth

```nohighlight
plot([1,10,3,9])

```

There’s a closed [ticket](https://github.com/JuliaPlots/Plots.jl/issues/4290) which mentions splines alongside `steppre` but splines doesn’t seem to get implemented at this pint.

---

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [October 15, 2022, 8:21pm UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/4 "2022-10-15T20:21:23Z")

</div>

Thanks, I presume this functionality isn’t available in Plots.

I saw that external packages could be used, but as in Python I can do it all within a plotting library I presumed the same would apply here.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [October 15, 2022, 8:53pm UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/5 "2022-10-15T20:53:03Z")

</div>

Honestly, this is a pretty heavy weight function for a plotting package to implement. If I was to do that, I would probably start by importing `Interpolations.jl` or some other spline package, and that would add an unacceptable dependency to Plots. It’s better to explicitly generate the spline and then plot it.

I am, however, a bit confused as to why `plot(b::Interpolations.Extrapolation)` is not by default equivalent to `plot(x->b(x))`, which would make this pretty much seamless. Nonetheless, here’s how to do what you want:

```julia
using Interpolations, Plots
y = [1, 10, 3, 9]
x = axes(y)
b = cubic_spline_interpolation(x, y)
plot(x, y; seriestype=:scatter)
plot!(x->b(x); seriestype=:path)

```

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [October 15, 2022, 10:39pm UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/6 "2022-10-15T22:39:58Z")

</div>

GMT.jl has a cubic splines _natively_. Here’s the example of the [sample1d](https://www.generic-mapping-tools.org/GMTjl_doc/documentation/modules/sample1d/index.html#sample1d) man page

```julia
using GMT
mat = [0 0; 1 1; 2 1.5; 3 1.25; 4 1.5; 4.5 3; 5 2; 6 2.5];
gmtbegin()
    D = sample1d(mat, inc=0.01, interp=:cubic);
    plot(D, region=(-0.1,6.1,-0.1,3.1), figsize=(14,6), xlabel=:u, ylabel="u(x)", lw=1, legend="Cubic spline (F=:c)")
    D = sample1d(mat, inc=0.01, interp=(:smooth, 1));
    plot!(D, lw=1, lc=:orange, legend="Smooth cubic spline (F=:s1)")
    D = sample1d(mat, inc=0.01, interp=:linear);
    plot!(D, lw=1, lc=:blue, legend="Linear spline (F=:l)")
    D = sample1d(mat, inc=0.01, interp=:akima);
    plot!(D, lw=1, lc=:red, legend="Akima spline (F=:a)")
    D = sample1d(mat, inc=0.01, interp=:nointerp);
    plot!(D, lw=1, lc=:darkgreen, legend="Nearest neighbor (F=:n)")
    plot!(mat, marker=:circ, ms=0.25, mc=:red, ml=:thin, legend="Data")
    legend(position=(inside=:TL, width=4.9, offset=0.2), box=(pen=1, fill=:white, shaded=true))
gmtend(:show)

```

 ![GMTplot](https://global.discourse-cdn.com/julialang/original/3X/1/a/1ae8bb717aad60fd84fd5739540c006cc3987cdf.png)

---

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [October 17, 2022, 6:40am UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/7 "2022-10-17T06:40:29Z")

</div>

This is the approach I went for in the time being, I’ll keep using this. It would be nice if this could somehow but provided in the docs.

---

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [October 17, 2022, 6:42am UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/8 "2022-10-17T06:42:21Z")

</div>

Thanks I’ll check out GMT, it seems particularly helpful for producing maps.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [October 17, 2022, 6:52am UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/9 "2022-10-17T06:52:24Z")

</div>

Maybe there is some domain where splines are essential. To me as a physicist, they are a poor replacement for a real curve fit, only really useful to give a false impression of resolution.

What I would do if I really wanted this to be simpler, is to make a feature request at Interpolations for a recipe or a documentation addition. This recipe would let you do `plot([1, 10, 3, 9]; seriestype=:spline)`, for instance:

```julia
@recipe function f(::Type{Val{:spline}}, x, y, z)
    @series begin
        primary := true
        seriestype := :scatter
        label := "Points"
        x, y
    end
    @series begin
        primary := false
        seriestype := :path
        r = range(extrema(x)...; length=101)
        b = cubic_spline_interpolation(x, y)
        label := "Spline"
        x := r
        y := b.(r)
    end
end

```

---

<div class="post-metadata">

### Author: ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)
#### Post date: [October 17, 2022, 7:14am UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/10 "2022-10-17T07:14:06Z")

</div>

I appreciate that, and often find they are not suitable. In this scenario I just wanted to make plot for a presentation using arbitrary values.

---

<div class="post-metadata">

### Author: ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)
#### Post date: [October 21, 2022, 11:30am UTC](https://discourse.julialang.org/t/plots-jl-spline/88720/11 "2022-10-21T11:30:26Z")

</div>

Also there are recipes defined in [GitHub - PumasAI/DataInterpolations.jl: A library of data interpolation and smoothing functions](https://github.com/PumasAI/DataInterpolations.jl)
