# Interpolation and Extrapolation in 2D in Python/v3

Learn how to interpolation and extrapolate data in two dimensions

Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.

#### New to Plotly?¶

Plotly's Python library is free and open source! Get started by dowloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

#### Imports¶

The tutorial below imports NumPy, Pandas, and SciPy.

In [1]:
import plotly.plotly as py
import plotly.graph_objs as go
from plotly.tools import FigureFactory as FF

import numpy as np
import pandas as pd
import scipy


#### Tips¶

Interpolation refers to the process of generating data points between already existing data points. Extrapolation is the process of generating points outside a given set of known data points.
(inter and extra are derived from Latin words meaning 'between' and 'outside' respectively)

#### Spline Interpolation¶

Interpolate for a set of points and generate the curve of best fit that intersects all the points.

In [2]:
from scipy import interpolate

x = np.arange(-5.0, 5.0, 0.25)
y = np.arange(-5.0, 5.0, 0.25)
xx, yy = np.meshgrid(x, y)
z = np.sin(xx**2+yy**2)
f = interpolate.interp2d(x, y, z, kind='cubic')

xnew = np.arange(-5.0, 5.0, 1e-1)
ynew = np.arange(-5.0, 5.0, 1e-1)
znew = f(xnew, ynew)

trace1 = go.Scatter3d(
x=x,
y=y,
z=z[0, :],
mode='markers',
name='Data',
marker = dict(
size = 7
)
)

trace2 = go.Scatter3d(
x=ynew,
y=xnew,
z=znew[0, :],
marker=dict(
size=3,
),
name='Interpolated Data'
)

layout = go.Layout(
title='Interpolation and Extrapolation in 2D',
scene=dict(
camera= dict(
up=dict(x=0, y=0, z=1),
center=dict(x=0, y=0, z=0),
eye=dict(x=1, y=-1, z=0)
)
)
)

data = [trace1, trace2]

fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='interpolation-and-extrapolation-2d')

Out[2]: