Show Sidebar Hide Sidebar # Matplotlib Time Series in matplotlib

How to make time series plots in Matplotlib with Plotly.

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 downloading 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!

#### Version Check¶

Plotly's python package is updated frequently. Run pip install plotly --upgrade to use the latest version.

In :
import plotly
plotly.__version__

Out:
'2.0.14'

### Basic Time Series Plot¶

In :
import datetime
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls

# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

x = np.array([datetime.datetime(2014, i, 9) for i in range(1,13)])
y = np.random.randint(100, size=x.shape)

plt.plot(x,y)
plt.tight_layout()

fig = plt.gcf()
plotly_fig = tls.mpl_to_plotly( fig )

py.iplot(plotly_fig, filename='mpl-time-series')

Out:

### Time Series With Custom Axis Rnage¶

In :
import datetime
import matplotlib.pyplot as plt
import numpy as np

import plotly.plotly as py
import plotly.tools as tls

# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

x = np.array([datetime.datetime(2014, i, 9) for i in range(1,13)])
y = np.random.randint(100, size=x.shape)

fig = plt.figure()

ax1.plot(x,y)
ax1.set_title('Setting Custom Axis  Range for time series')

plotly_fig = tls.mpl_to_plotly( fig )

plotly_fig['layout']['xaxis1']['range'] = [1357669800000, 1449599400000]

py.iplot(plotly_fig, filename='mpl-time-series-custom-axis')

Out:

#### Reference¶ 