Show Menu
Cheatography

Python Basics - Numpy and Matplotlib Cheat Sheet (DRAFT) by

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Numpy - Functions

import numpy as np
Numpy import­ation
np.sin(0)
Returns sine of 0 (0)
np.cos(0)
Returns cosine of 0 (1)
np.sinh(0)
Returns hiperbolic sine of 0 (0)
np.cosh(0)
Returns hiperbolic cosine of 0 (1)
np.pi
Returns the value of pi (3.141­5...)
np.arr­ay([1, 2, 3])
Returns a 1-dime­nsional array
np.array([[1, 2],[3, 4]])
Returns a 2-dime­nsional array
np.mat­([[1, 2],[3, 4]])
Returns a matrix
np.lin­spa­ce(0, 1, 11)
Returns a 1-dime­nsional array from 0 to 1 with 11 elements
np.log­spa­ce(1, 3, 11)
Returns a 1-dime­nsional array from 101 to 103 with 11 elements
np.ara­nge(0, 1, 0.1)
Returns a 1-dime­nsional array from 0 to 1 with step 0.1
np.ran­dom.ra­ndo­m((2)
Returns a random 1-dime­nsional array with 2 elements
np.ran­dom.ra­ndo­m((2, 2))
Returns a random 2-dime­nsional array with 2 rows and 2 columns
np.random.random((2, 2, 2))
Returns a random 3-dime­nsional array with 2 elements in each dimension
np.eye(5)
Returns a 5x5 identity matrix
np.zer­os(5)
Returns a null vector with 5 elements
np.zer­os((5, 2))
Returns a null matrix with 5 rows and 2 columns
np.ones(5)
Returns a 5 elements vector filled with 1
np.one­s((5, 2))
Returns a 5x2 matrix filled with 1
1Numpy has all functions and constants in the library Math.
2Numpy arrays can be used to do all sorts of linear algebra calcul­ations since they are treated as mathem­atical tensors (vectors and matrices) rather than Python lists.
3Numpy matrices are a special type of array that is easier to use in common linear algebra problems. Matrices are always 2D

Numpy - Linalg

np.lin­alg.dot(a, b)
Returns the dot product between arrays a and b
np.linalg.matmul(a, b)
Returns the matrix product between arrays a and b
np.lin­alg.ei­gva­ls(a)
Returns the eigenv­alues of the square array a
np.lin­alg.eig(a)
Returns the eigenv­alues and eigenv­ectors of the square array a
np.lin­alg.no­rm(a)
Returns the a array norm
np.lin­alg.det(a)
Returns the determ­inant of a
np.lin­alg.inv(a)
Returns the inverse of a
np.lin­alg.pi­nv(a)
Returns the pseudo­-in­verse of a
np.linalg.solve(a, b)
Returns the solution of aX = b

Numpy - Slicing

import numpy as np

a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
a[1:, 1:] = [[50, 60], [80, 90]]
print(a)
[[ 1 2 3]
[ 4 50 60]
[ 7 80 90]]

Matplotlib - Example

from matplotlib import pyplot as plt
import numpy as np

x = np.linspace(0, 10, 200)
y1 = 3*np.sin(x) + np.cos(x)
y2 = 2*np.cos(x) - np.sin(x)

plt.plot(x, y1, '-b', label="y1")
plt.plot(x, y2, '-r', label="y2")
plt.xlabel("x")
plt.ylabel("y")
plt.title("Y Vs. X")
plt.legend()
plt.grid()
plt.show()

Matplotlib - Example (Output)

Matplotlib - Pyplot Functions

from matplotlib import pyplot as plt
Pyplot import­ation
plt.pl­ot(x, y[,args])
Plots x Vs. y on current figure
plt.xl­abe­l("x­")
Sets x-axis label to x
plt.yl­abe­l("y­")
Sets y-axis label to y
plt.ti­tle­("Y Vs. X")
Sets figure title to Y Vs. X
plt.le­gend()
Shows legends in figure
plt.grid()
Shows grid in figure
plt.show()
Shows figures
plt.fi­gure()
Starts a new figure
1 Many args can be set on plt.plot(). Some are shown on the next block.

Pyplot - Lines, Markers and Colors

'-'
solid line style
'--'
dashed line style
'-.'
dash-dot line style
':'
dotted line style
'.'
point marker
','
pixel marker
'o'
circle marker
'v'
triang­le_down marker
'^'
triang­le_up marker
'<'
triang­le_left marker
'>'
triang­le_­right marker
's'
square marker
'p'
pentagon marker
'*'
star marker
'h'
hexagon1 marker
'H'
hexagon2 marker
'+'
plus marker
'x'
x marker
'D'
diamond marker
'd'
thin_d­iamond marker
'|'
vline marker
'_'
hline marker
'b'
blue
'g'
green
'r'
red
'c'
cyan
'm'
magenta
'y'
yellow
'k'
black
'w'
white
You can use a line style, a marker and a color directly at plt.plot() like this:
plt.pl­ot(x, y, '-+b')