Matplotlib

Matplotlib

Displaying Plots

Matplotlib Script Example

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

plt.show()

Matplotlib IPython Example

%matplotlib

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
fig = plt.figure() # create a plot window
plt.plot(x, np.sin(x))
plt.plot(x, np.cos(x))

MATLAB-style Interface Example

plt.figure() # create a plot

# create a first panel and set the axis
plt.subplot(2, 1, 1) # (rows, columns, panel #)
plt.plot(x, np.sin(x))

# create a second panel and set the axis
plt.subplot(2, 1, 2)
plt.plot(x, np.cos(x))

Object-oriented Interface Example

# create a grid of subplots
fig, ax = plt.subplots(2)
# fig is the figure
# ax is an array of Axes objects

# Call the plot method on each Axes object
ax[0].plot(x, np.sin(x))
ax[1].plot(x, np.cos(x))

Basic Plot Customization

Line Color

Line Style

Axes Limits

Labels

Discrepancies Between Interfaces

Scatter Plots

Another plot Example

Another scatter Example

Saving Figures to a File