import matplotlib.pyplot as pl
import numpy as np
☑ Plotting
1 Matplotlib
We will be covering key plotting APIs that will be very useful for visualization of machine learning data.
https://medium.com/mlpoint/matplotlib-for-machine-learning-6b5fcd4fbbc7
#
# an empty figure
#
= pl.figure(figsize=(3, 3))
fig 'X labels go here')
pl.xlabel('Y labels go here')
pl.ylabel(0,1,2,3,4,5])
pl.xticks([
#
# get the axis object to set the tick labels
#
= pl.gca()
ax 'a', 'b', 'c', 'd', 'e', 'f']); ax.set_xticklabels([
=(6,6))
pl.figure(figsize
= np.linspace(-2*np.pi, 2*np.pi, 1000)
x = np.sin(x)
y1 = np.cos(x)
y2
='red', linestyle='-')
pl.plot(x, y1, color='blue', linestyle='--')
pl.plot(x, y2, color
'y1=sin(x)', 'y2=cos(x)'], loc='upper right'); pl.legend([
2 Line plots
#
# Plotting line with annotation
#
= np.linspace(-1, 1, 20)
x = np.abs(np.random.randn(20))
y1 '-o')
pl.plot(x, y1,
10], y1[10], 'midpoint', fontsize=12); pl.text(x[
#
# Area plot
#
='blue', alpha=0.1)
pl.fill_between(x, y1, color=2, color='blue'); pl.plot(x, y1, linewidth
= - y1 / 2
y2
='gray', alpha=0.5);
pl.fill_between(x, y1, y2, color='black', linewidth=2); pl.plot(x, y1, x, y2, color
3 Bar Plots
= ['Jack', 'Jill', 'Joe']
names = np.array([0, 1, 2])
x = [70, 75, 80]
grades
pl.bar(x, grades); pl.xticks(x, names)
= [95, 95, 90]
grades_winter
+0.00, grades, width=0.25, label='Fall')
pl.bar(x+0.25, grades_winter, width=0.25, label='Winter')
pl.bar(xNone, 120)
pl.ylim(='upper left'); pl.legend(loc
#
# final, midterm, quiz
#
= np.array([
grades 30, 30, 10],
[50, 30, 15],
[45, 23, 5],
[
])
0], label='final')
pl.bar(x, grades[:, 1], bottom=grades[:, 0], label='midterm', )
pl.bar(x, grades[:, 2], bottom=grades[:, :2].sum(axis=1), label='quiz')
pl.bar(x, grades[:,
pl.legend(); pl.xticks(x, names)
4 Scatter Plots
= 500
n = np.random.randn(n)
x = np.random.randn(n)
y =10); pl.scatter(x, y, s
#
# with size variation
#
= np.abs(np.random.randn(n)) * 30
size =size, alpha=0.4, linewidth=0); pl.scatter(x, y, s
#
# with color variation
#
= np.full((n,), '#f00')
colors <= y] = '#0f0'
colors[x
=size, c=colors, alpha=0.4, edgecolors='none'); pl.scatter(x, y, s
5 More to come
As we proceed further with the topics of machine learning, we will encounter more mathematical objects and ways to show them graphically.
We will look at:
- generate meshgrids
- contour plots of potential functions
- quiver plots for field functions
- 3D plots for higher dimensional datasets
6 Contours
Suppose we have the following potential function over the 2D space.
\[ z = \sin(\sqrt{x^2 + y^2}) \]
We want to compute the potential function in the region of \([-10, 10]\) by \([-10, 10]\).
= np.linspace(-10, 10, 100)
xs = np.linspace(-10, 10, 100)
ys = np.zeros((100, 100)) zs
Warning
Do not use loop.
#
# You should not be doing this.
#
for i in range(xs.size):
for j in range(ys.size):
= np.sin(np.sqrt(xs[i]**2 + ys[j]**2))
zs[i,j]
zs.shape
(100, 100)
Note
The proper way is to use meshgrid.
= np.meshgrid(xs, ys)
X, Y X.shape, Y.shape
((100, 100), (100, 100))
= np.sin(np.sqrt(X**2 + Y**2))
Z Z.shape
(100, 100)
=15, cmap='gray') pl.contour(X, Y, Z, levels
<matplotlib.contour.QuadContourSet at 0x7ff4e1f9b820>
=15, cmap='jet'); pl.contourf(X, Y, Z, levels
=[np.min(Z), np.mean(Z), np.max(Z)], cmap='jet'); pl.contourf(X, Y, Z, levels
7 Vector fields
= np.meshgrid(
X, Y -5, 5, 10),
np.linspace(-5, 5, 10)
np.linspace(
)
= 1
u = -1
v
='white'); pl.quiver(X, Y, u, v, color
= X
u = Y
v
='white'); pl.quiver(X, Y, u, v, color
Here is a call vector field:
\[ \vec F(x, y) = -\frac{y}{\sqrt{x^2+y^2}}\mathbf{i} + \frac{x}{\sqrt{x^2+y^2}}\mathbf{j} \]
= -Y / np.sqrt(X**2 + Y**2)
u = X / np.sqrt(X**2 + Y**2)
v
='white'); pl.quiver(X, Y, u, v, color
8 3D plot points and lines
from mpl_toolkits import mplot3d
= pl.figure()
fig = pl.axes(projection='3d') ax
= pl.axes(projection='3d')
ax
= np.linspace(0, 15, 1000)
zs = np.sin(zs)
xs = np.cos(zs)
ys
=2, color='red');
ax.plot3D(xs, ys, zs, linewidth
= np.random.random(100) * 15
z2 = np.sin(z2) + 0.1 * np.random.random(100)
x2 = np.cos(z2) + 0.1 * np.random.random(100)
y2 =z2, cmap='jet'); ax.scatter(x2, y2, z2, c
9 3D Contours
= np.linspace(-6, 6, 30)
xs = np.linspace(-6, 6, 30)
ys
= np.meshgrid(xs, ys)
X, Y = np.sin(np.sqrt(X ** 2 + Y ** 2)) Z
X.shape, Y.shape, Z.shape
((30, 30), (30, 30), (30, 30))
= pl.axes(projection='3d')
ax =100, cmap='jet')
ax.contour3D(X, Y, Z, levels'x')
ax.set_xlabel('y')
ax.set_ylabel('z');
ax.set_zlabel(
## ax.view_init(60, 35);
#
#
#