{}
import numpy as np
import matplotlib.pyplot as pl
x = np.linspace(-5, 5, 100)
y = np.sin(x)
pl.plot(x, y);
# We can control the styling
pl.plot(x, y, color='r', linestyle='--');
#
# We can superimpose multiple lines
#
x2 = np.linspace(-5, 5, 10)
y2 = np.sin(x2)
pl.plot(x, y, color='r')
pl.plot(x2, y2, marker='o', linestyle="");
#
# We can also add text annotations
#
x1 = 2.1; y1 = np.sin(x1)
x2 = np.pi + 0.1; y2 = np.sin(x2)
pl.plot(x, y)
pl.text(x1, y1, 'higher')
pl.text(x2, y2, 'lower')
#
# Manually set the limits
#
x1 = 2.1; y1 = np.sin(x1)
x2 = np.pi + 0.1; y2 = np.sin(x2)
pl.plot(x, y)
pl.text(x1, y1, 'higher')
pl.text(x2, y2, 'lower')
pl.xlim(-10, 10)
pl.ylim(-2, 2);
xs = np.linspace(-5, 5, 100)
def f1(x):
return x * x
def f2(x):
return 3*x + 4
pl.plot(xs, f1(xs))
pl.plot(xs, f2(xs))
pl.plot(xs, f1(xs))
pl.plot(xs, f2(xs))
pl.fill_between(xs, f1(xs), f2(xs), color='gray');
pl.plot(xs, f1(xs))
pl.plot(xs, f2(xs))
pl.fill_between(xs, f1(xs), f2(xs), where=f1(xs) < f2(xs), color='gray');
#
# A limitation
#
x1 = np.linspace(-5, 5, 40)
x2 = np.linspace(-4, 4, 80)
y1 = np.sin(x1)
y2 = np.cos(x2)
pl.plot(x1, y1, marker='o')
pl.plot(x2, y2, linestyle='--', marker='o');
#
# This fails because y2 and y1 are different lengths.
#
pl.plot(x1, y1)
pl.plot(x2, y2, linestyle='--')
pl.fill_between(x1, y1, y2)
y1_interp = np.interp(x2, x1, y1)
y1_interp.shape
pl.plot(x1, y1)
pl.plot(x2, y2, linestyle='--')
pl.fill_between(x2, y1_interp, y2, where=y1_interp < y2)
xs = np.linspace(-5, 5, 10)
ys = np.sin(xs)
errs = np.random.random(len(xs))
errs
#
# Each point has error 2*errs.
#
pl.errorbar(xs, ys, yerr=errs, capsize=3);
#
# Asymmetric errors
#
err_low = np.random.random(len(xs)) * 0.2
err_hi = np.random.random(len(xs)) * 0.1
errs = np.array([err_low, err_hi])
errs
pl.errorbar(xs, ys, yerr=errs, capsize=0, elinewidth=5, color='gray')
x = np.linspace(-1, 1, 5)
y1 = np.random.random(len(x))
y2 = np.random.random(len(x))
pl.bar(x, y1, width=0.2);
#
# Side-by-side plots
#
pl.bar(x, y1, width=0.2)
pl.bar(x+0.2, y2, width=0.2);
#
# Adjust bottom
#
pl.bar(x, y2, width=0.2, bottom=0.5);
pl.ylim(0, 2)
#
# Adjust bottom
#
pl.bar(x, y1, width=0.2)
pl.bar(x, y2, width=0.2, bottom=y1);
pl.ylim(0, 2)
Consider a 2D grid
(x1,y1) (x2,y1) (x3,y1) ... (xn, y1)
(x1,y2) (x2,y2) (x3,y3) ... (xn, y2)
.
.
.
(x1,ym) (x2,ym) (x3,ym) ... (xn,ym)
At each coordinate (xi,yj), we can have height z[i,j].
Contour plot visualizes the heights as a 2D plot.
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
xgrid, ygrid = np.meshgrid(x, y)
xgrid.shape
z = xgrid * xgrid + ygrid * ygrid
z.shape
colormap = pl.get_cmap('jet')
pl.contourf(xgrid, ygrid, z, levels=10, cmap=colormap);
pl.contour(xgrid, ygrid, z, colors='gray');
from mpl_toolkits.mplot3d import Axes3D
ax = pl.gca(projection ='3d')
ax.plot_wireframe(xgrid, ygrid, z);
#
# Default view is:
# elevation = 30
# azimuth = 45
#
elevation = 0
azimuth = 45 - 30
ax = pl.gca(projection ='3d')
ax.view_init(elevation, azimuth)
ax.plot_wireframe(xgrid, ygrid, z);