This file contains a Matplotlib implementation of the examples provided in the Examples.ipynb notebook, for easy comparison.

Let's go!

First import the required modules.


In [1]:
%matplotlib notebook

In [3]:
# Also import numpy as it will be useful…
import numpy as np

# and add some black magic for easy reloading of the module before executing any cell (just here to ease testing)
%load_ext autoreload
%autoreload 2

# ignore matplotlib warnings about too many figures in the notebook
import matplotlib as mpl
mpl.rcParams["figure.max_open_warning"] = 50
import matplotlib.pyplot as plt


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

Basic plotting

Let's start by doing some basic plotting.


In [9]:
# Let's plot a basic data series
fig, ax = plt.subplots()
x = range(10)
ax.plot(x)
fig.show()



In [10]:
# Or a (X, Y) point series
fig, ax = plt.subplots()
x = range(10)
y = [i**2 for i in x]
ax.plot(x, y)
fig.show()



In [15]:
# Plotting a discrete (X, Y) point series
fig, ax = plt.subplots()
x = range(10)
y = [i**2 for i in x]
ax.scatter(x, y)
fig.show()
    
# or equivalently plotting a discrete (X, Y) point series
fig2, ax2 = plt.subplots()
x = range(10)
y = [i**2 for i in x]
ax2.scatter(x, y)
fig2.show()



In [17]:
# Plotting broken lines
fig, ax = plt.subplots()
x = [1, 2, 3, 2, 1]
y = [1, 1, 2, 2, 1]
ax.plot(x, y)
fig.show()



In [22]:
# Plotting a function
fig, ax = plt.subplots()
x = np.linspace(-2, 2, 100)
y = np.sin(x)
ax.plot(x, y)  # Plot sinus on [-2, 2]
fig.show()



In [23]:
# And we can plot multiples graphs on the same figure
fig, ax = plt.subplots()
ax.plot(range(5))
for i in range(8):
    x = np.linspace(-10, 10, 100)
    y = [np.sin(abscissa + np.pi * i / 4) for abscissa in x]
    ax.plot(x, y)
fig.show()


Advanced plotting

We can do more elaborated stuff.


In [25]:
# Plot with a label on the axis, and a title for the figure
fig, ax = plt.subplots()
x = range(10)
ax.plot(x, x, label="x")
ax.set_xlabel("some x label")
ax.set_ylabel("some y label")
ax.set_title("A title for the figure")
fig.show()



In [28]:
fig, ax = plt.subplots()
ax.set_xlabel("some x label")
ax.set_ylabel("some y label")
ax.set_title("A title for the figure")
x = np.linspace(-10, 10, 100)
y = np.sin(x)
ax.plot(x, y, label="sin")
ax.legend("lower right")  # Not that ax.legend call position matters
fig.show()



In [30]:
fig, ax = plt.subplots()
ax.set_xlabel("some x label")
ax.set_ylabel("some y label")
ax.set_title("A title for the figure")
ax.set_xlim(-20, 10)
ax.set_ylim(-3, 3)
x = np.linspace(-10, 10, 100)
y = np.sin(x)
ax.plot(x, y, label="sin")
ax.legend("lower right")  # Not that ax.legend call position matters
fig.show()



In [31]:
# Passing extra arguments to matplotlib plot command
fig, ax = plt.subplots()
ax.set_xlabel("some x label")
ax.set_ylabel("some y label")
ax.set_title("A title for the figure")
ax.set_xlim(-20, 10)
ax.set_ylim(-3, 3)
x = np.linspace(-10, 10, 100)
y = np.sin(x)
ax.plot(x, y, label="sin", linewidth=20)
ax.legend("lower right")  # Not that ax.legend call position matters
fig.show()



In [42]:
import seaborn.apionly as sns
# You can also tweak the palette used, either with a seaborn palette
sns.set_palette(sns.color_palette("dark", 10))
fig, ax = plt.subplots()
x = np.linspace(-10, 10, 100)
y = np.sin(x)
ax.plot(x, y)
fig.show()
sns.reset_orig()



In [37]:
# To plot in log scale
fig, ax = plt.subplots()
x = np.linspace(1, 10, 100)
y = np.log(x)
ax.semilogx(x, y)
fig.show()



In [34]:
# Same for log-log scale
fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
y = x**2
ax.loglog(x, y)
fig.show()



In [36]:
# To force an orthonormal frame, use orthonormal=True on the plot
fig, ax = plt.subplots()
x = np.linspace(1, 10, 100)
y = np.log(x)
ax.plot(x, y)
ax.axis("equal")
fig.show()


Using subplots


In [45]:
# Applying a grid on a figure, one empty subplot
ax = plt.subplot2grid((1, 2), (0, 0))
x = np.linspace(-10, 10, 100)
y = np.cos(x)
ax.plot(x, y)
plt.show()



In [46]:
ax1 = plt.subplot2grid((3,3), (0,0), colspan=3)
ax2 = plt.subplot2grid((3,3), (1,0), colspan=2)
ax3 = plt.subplot2grid((3,3), (1, 2), rowspan=2)
ax4 = plt.subplot2grid((3,3), (2, 0))
ax5 = plt.subplot2grid((3,3), (2, 1))
x = np.linspace(-10, 10, 100)
y = np.cos(x)
ax1.plot(x, y)
ax2.plot(x, y)
ax3.plot(x, y)
ax4.plot(x, y)
ax5.plot(x, y)
plt.show()


Saving figures


In [39]:
# Save a figure to a file
fig, ax = plt.subplots()
x = np.linspace(1, 10, 100)
y = np.log(x)
ax.plot(x, y)
fig.savefig("/tmp/out.png")