# Numerical Differentiation in Python/v3

Learn how to differentiate a sequence or list of values numerically

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


#### Differentiate the Sine FunctionÂ¶

How to use numerical differentiation to plot the derivative of the sine function $y = sin(x)$:

In [2]:
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

dy = np.zeros(y.shape,np.float)
dy[0:-1] = np.diff(y)/np.diff(x)
dy[-1] = (y[-1] - y[-2])/(x[-1] - x[-2])

trace1 = go.Scatter(
x=x,
y=y,
mode='lines',
name='sin(x)'
)

trace2 = go.Scatter(
x=x,
y=dy,
mode='lines',
name='numerical derivative of sin(x)'
)

trace_data = [trace1, trace2]
py.iplot(trace_data, filename='numerical-differentiation')

Out[2]: