# How to make interpolated lines in Python & Matplotlib

--

Bit of backstory: I was doing a project that required plotting an interpolated line between points and I couldn’t find any good tutorials on the topic, just a few docs. I eventually figured it out and it’s actually pretty easy, so I am writing this blog for posterity.

NOTE: You’re much better off making plots using plotly, it’s much easier and it integrates with Dash if you ever want to build a web app. You can make an interpolated line in plotly with 1 simple line. However, if you’re hellbent on making this plot in Matplotlib, read on.

An interpolated line is very useful for data visualizations. Imagine you have the below dataframe.

`import pandas as pddata = {'Stage': list(range(1, 6)),        'Heart Rate': [120, 125, 140, 160, 200]        }df = pd.DataFrame(data)`

Now let us plot this dataframe, with Stage as the x-axis and Heart Rate as the y-axis.

`import matplotlib.plot as pltplt.plot(df['Stage'], df['Heart Rate'])plt.show()`

Matplotlib is interpolating a line between the points, but it is a straight line. We can remove this interpolated line and view only the points with the below code.

`plt.plot(df['Stage'], df['Heart Rate'], marker='.', linestyle='none')plt.show()`

But what if we want to make it a curved line? Enter scipy.interpolate

Scipy.interpolate is a package that can be used to interpolate lines between points in whatever fashion you want. For this plot, we will be using the interp1d function from the scipy.interpolate package, which will allow us to interpolate 1-dimensional lines. The interp1d function requires that you enter the set of values between which you would like to interpolate. For us, that is `df` , or specifically `df['Stage']` as the x values and `df['Heart Rate']` as the y values. Since we want a curved interpolation line, we will choose the `cubic` kind, as seen in the code below.

`from scipy.interpolate import interp1df = interp1d(df['Stage'], df['Heart Rate'], kind='cubic')`

f is our new function, which we have created using the interp1d function. If you enter any x-value between 1 and 5, it will return a y-value that is interpolated using cubic interpolation. If you enter any of the values from `df['Stage']` it will return the exact corresponding value from `df['Heart Rate']` . We can test this using the below code.

`plt.plot(df['Stage'], f(df['Stage']), marker='.', linestyle='none')plt.show()`

As you can see, the plot is exactly the same. What would happen if we made a new array of 50 values evenly spaced between 1 and 5, and used that to make a plot?

`import numpy as npxnew = np.linspace(1, 5, num=50)plt.plot(xnew, f(xnew), marker='.', linestyle='none')plt.show()`

Now let’s add back the linear interpolation between points which matplotlib does automatically.

`plt.plot(xnew, f(xnew))plt.show()`

There’s our interpolated line. If you want to explore other interpolation methods, check out the documentation.

What if we want to keep the dots for the actual datapoints and show the interpolated line going through them? For this we put 2 lines on the graph, 1 line has the 5 real datapoints and only shows dots, the other line has 50 datapoints and only shows the line.

`plt.plot(df['Stage'], f(df['Stage']), marker='.', linestyle='none')plt.plot(xnew, f(xnew))plt.show()`

And there you have it. If you have any more questions leave a comment below or reach out to me.

--

--

0 Followers

Software Developer & Data Scientist based in Dublin, Ireland.