Show Sidebar Hide Sidebar

Average Multiple Curves in Python

Learn how to average the values of multiple curves with Python.

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

Average of 2 Curves

Given two curves defined by functions $f$ and $g$ on $\mathbb{R} \rightarrow \mathbb{R}$, the average curve $h$ of $f$ and $g$ is defined by $h = \frac{f(x) + g(x)}{2} $ for $x \in \mathbb{R}$.

In [2]:
x = np.linspace(0, 2*np.pi, 100)
f = np.sin(x)
g = np.cos(x)
h = [(f[j] + g[j])/2 for j in range(len(x))] 

trace1 = go.Scatter(
    x=x,
    y=f,
    mode='lines',
    name='f(x)',
    marker=dict(
        color='rgb(220, 20, 60)'
    )
)

trace2 = go.Scatter(
    x=x,
    y=g,
    mode='lines',
    name='g(x)',
    marker=dict(
        color='rgb(100, 149, 237)'
    )
)

trace3 = go.Scatter(
    x=x,
    y=h,
    mode='markers+lines',
    name='Average of f and g',
    marker=dict(
        color='rgb(128, 0, 128)',
        symbol='diamond-open',
    )
)

data = [trace1, trace2, trace3]
py.iplot(data, filename='2-curves')
Out[2]:
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.