Show Sidebar Hide Sidebar

Line Charts in matplotlib

How to make a plot in matplotlib. Examples of the plot function, line and marker types, custom colors, and log and semi-log axes.

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!

IF you're adding a doc about a new feature, the second and third cells will display a version check. This is to ensure the

Version Check

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

In [1]:
import plotly
plotly.__version__
Out[1]:
'3.1.1'

Matplotlib basic Line Chart Example with Legend

In [ ]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10)
plt.plot(x, np.sin(x), '--', linewidth=2)
plt.plot(x, np.cos(x), '--', linewidth=2)

fig = plt.gcf()

plotly_fig = tls.mpl_to_plotly(fig)
plotly_fig['layout']['showlegend'] = True

fig = plt.gcf()
py.iplot(plotly_fig)

Different colored lines for different plots

Inspired by Stack Overflow

In [ ]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np

colormaps_fig = plt.figure()

num_plots = 10

# Have a look at the colormaps here and decide which one you'd like:
# http://matplotlib.org/1.2.1/examples/pylab_examples/show_colormaps.html
colormap = plt.cm.gist_ncar
plt.gca().set_color_cycle([colormap(i) for i in np.linspace(0, 0.9, num_plots)])

# Plot several different functions...
x = np.arange(10)
labels = []
for i in range(1, num_plots + 1):
    plt.plot(x, i * x + 5 * i)
    labels.append(r'$y = %ix + %i$' % (i, 5*i))

plotly_fig = tls.mpl_to_plotly(colormaps_fig)
py.iplot(plotly_fig)

Sine and Cosine graph

Inspired by Stack Overflow

In [ ]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import pylab
import numpy as np


legend_fig = plt.figure()

x = np.linspace(0, 20, 1000)
y1 = np.sin(x)
y2 = np.cos(x)

pylab.plot(x, y1, '-b', label='sine')
pylab.plot(x, y2, '-r', label='cosine')
pylab.ylim(-1.5, 2.0)

plotly_fig = tls.mpl_to_plotly(legend_fig)
py.iplot(plotly_fig)

Density plot

Inspired by Stack Overflow

In [ ]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde

mpl_density = plt.figure()

data = [1.5]*7 + [2.5]*2 + [3.5]*8 + [4.5]*3 + [5.5]*1 + [6.5]*8
density = gaussian_kde(data)
xs = np.linspace(0,8,200)
density.covariance_factor = lambda : .25
density._compute_covariance()
plt.plot(xs,density(xs))

plotly_fig = tls.mpl_to_plotly(mpl_density)
py.iplot(plotly_fig)

Logarithmic axes

Inspired by Stack Overflow

In [ ]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib.pyplot as plt
from matplotlib import pyplot
import numpy as np

x = np.arange(0.01,50,0.1)

fig,(ax1,ax2) = plt.subplots(2)

ax1.plot(x,np.exp(50+x/50.))
ax1.set_xlim(x[-1],x[0])
ax1.set_title('plot')

ax2.semilogx(x,np.log(100*x))
ax2.set_xlim(x[-1],x[0])

plotly_fig = tls.mpl_to_plotly(fig)
py.iplot(plotly_fig)

Matplotlib Colormap

Inspired by Stack Overflow

In [ ]:
import plotly.plotly as py
import plotly.tools as tls

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

mpl_color = plt.figure()

# Generate data...
nx, nsteps = 100, 20
x = np.linspace(0, 1, nx)
data = np.random.random((nx, nsteps)) - 0.5
data = data.cumsum(axis=0)
data = data.cumsum(axis=1)

# Plot
cmap = mpl.cm.autumn
for i, y in enumerate(data.T):
    plt.plot(x, y, color=cmap(i / float(nsteps)))

plotly_fig = tls.mpl_to_plotly(mpl_color)
py.iplot(plotly_fig)

Reference

See https://plot.ly/python/reference/ for more information and chart attribute options!

Still need help?
Contact Us

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