Matplotlib CheatSheet

Matplotlib is used to plot graphs so it is often used alongside Numpy.

Always use:

import matplotlib.pyplot as plt

import numpy as np (assuming numpy is always used with matplotlib)

  • Plotting a 1D array:

plt.plot(xarray,yarray)

plt.show()

  • Plotting a 2D array:

Colour images:

image = np.array([[0,1,2],[3,4,5]])

plt.imshow(image, cmap = plt.cm.jet)      # creates the image based on the array image and  #cmap (colour map) jet

plt.colorbar()  #creates a legend bar

plt.show()  #displays the plot

  • Adding labels to the graphs:

plt.xlabel(“Insert label for x axis here”)

plt.ylabel(“Insert label for y axis here”)

http://matplotlib.org/users/pyplot_tutorial.html – Pyplot tutorial

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s