Show Sidebar Hide Sidebar

Matplotlib Time Series in matplotlib

How to make time series plots in Matplotlib with Plotly.

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 [8]:
import plotly
plotly.__version__
Out[8]:
'2.0.14'

Basic Time Series Plot

In [9]:
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[9]:

Time Series With Custom Axis Rnage

In [10]:
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 = fig.add_subplot(111)

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[10]:

Reference

Still need help?
Contact Us

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