Show Sidebar Hide Sidebar

plot | 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.

import matplotlib.pyplot as plt
import numpy as np

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

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()
plot_url = py.plot_mpl(fig, filename='mpl-basic-line')
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import matplotlib.pyplot as plt
import numpy as np
import plotly.plotly as py

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))
    
plot_url = py.plot_mpl(colormaps_fig, filename='mpl-colormaps')
Inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import numpy as np
import pylab 
import plotly.plotly as py

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)

plot_url = py.plot_mpl(legend_fig, filename='mpl-sine-cosine')
Inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import plotly.plotly as py
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))

plot_url = py.plot_mpl(mpl_density, filename='mpl-density')
Inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

import matplotlib.pyplot as plt
from matplotlib import pyplot
import plotly.plotly as py

log_fig = plt.figure()

a = [ pow(10,i) for i in range(10) ]

pyplot.subplot(2,1,1)
pyplot.plot(a, color='blue', lw=2)
pyplot.yscale('log')
pyplot.show()

plot_url = py.plot_mpl(log_fig, filename='mpl-log')
Inspired by Stack Overflow.
# Learn about API authentication here: https://plot.ly/python/getting-started
# Find your api_key here: https://plot.ly/settings/api

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

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)))

plot_url = py.plot_mpl(mpl_color, filename='mpl-color-example')
Inspired by Stack Overflow.
Still need help?
Contact Us

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