Introduction to Plotting (Matplotlib Basics)¶
Import¶
In [2]:
Copied!
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
Basic Line Plot¶
In [3]:
Copied!
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
plt.plot(x, y)
plt.show()
x = np.linspace(0, 2*np.pi, 200)
y = np.sin(x)
plt.plot(x, y)
plt.show()
With Labels and Title¶
In [4]:
Copied!
plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("sin(x)")
plt.title("Plot of sin(x)")
plt.show()
plt.plot(x, y)
plt.xlabel("x")
plt.ylabel("sin(x)")
plt.title("Plot of sin(x)")
plt.show()
Add Grid and Legend¶
In [5]:
Copied!
plt.plot(x, y, label="sin(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)
plt.legend()
plt.show()
plt.plot(x, y, label="sin(x)")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)
plt.legend()
plt.show()
Customize Line and markers:¶
In [6]:
Copied!
plt.plot(x, y, color="red", linestyle="--", linewidth=2)
plt.show()
plt.plot(x, y, color="red", linestyle="--", linewidth=2)
plt.show()
In [7]:
Copied!
plt.plot(x, y, marker="o", markersize=4)
plt.show()
plt.plot(x, y, marker="o", markersize=4)
plt.show()
Multiple Curves¶
In [8]:
Copied!
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label="sin(x)")
plt.plot(x, y2, label="cos(x)")
plt.legend()
plt.grid(True)
plt.show()
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label="sin(x)")
plt.plot(x, y2, label="cos(x)")
plt.legend()
plt.grid(True)
plt.show()
Change Figure Size¶
In [9]:
Copied!
plt.figure(figsize=(4,2))
plt.plot(x, np.sin(x))
plt.show()
plt.figure(figsize=(4,2))
plt.plot(x, np.sin(x))
plt.show()
Save Figure¶
In [10]:
Copied!
plt.plot(x, np.sin(x))
plt.savefig("sine_plot.png")
plt.plot(x, np.sin(x))
plt.savefig("sine_plot.png")
Scatter Plot¶
In [11]:
Copied!
x = np.linspace(0, 10, 30)
y = x + np.random.randn(30)
plt.scatter(x, y)
plt.show()
x = np.linspace(0, 10, 30)
y = x + np.random.randn(30)
plt.scatter(x, y)
plt.show()
Shade Between Two Functions¶
In [12]:
Copied!
x = np.linspace(0, 2*np.pi, 400)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label="sin(x)")
plt.plot(x, y2, label="cos(x)")
plt.fill_between(x, y1, y2, alpha=0.3)
plt.legend()
plt.grid(True)
plt.show()
x = np.linspace(0, 2*np.pi, 400)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label="sin(x)")
plt.plot(x, y2, label="cos(x)")
plt.fill_between(x, y1, y2, alpha=0.3)
plt.legend()
plt.grid(True)
plt.show()
In [13]:
Copied!
exact = np.sin(x)
numerical = np.sin(x) + 0.1*np.sin(5*x)
plt.plot(x, exact, label="Exact")
plt.plot(x, numerical, label="Numerical")
plt.fill_between(x, exact, numerical, alpha=0.3)
plt.legend()
plt.grid(True)
plt.show()
exact = np.sin(x)
numerical = np.sin(x) + 0.1*np.sin(5*x)
plt.plot(x, exact, label="Exact")
plt.plot(x, numerical, label="Numerical")
plt.fill_between(x, exact, numerical, alpha=0.3)
plt.legend()
plt.grid(True)
plt.show()
Bar Plot¶
In [14]:
Copied!
categories = ["A", "B", "C", "D"]
values = [10, 15, 7, 12]
plt.bar(categories, values)
plt.show()
categories = ["A", "B", "C", "D"]
values = [10, 15, 7, 12]
plt.bar(categories, values)
plt.show()
Histogram¶
In [15]:
Copied!
data = np.random.randn(1000)
plt.hist(data, bins=20)
plt.show()
data = np.random.randn(1000)
plt.hist(data, bins=20)
plt.show()
Subplots¶
In [16]:
Copied!
x = np.linspace(0, 2*np.pi, 200)
plt.subplot(1, 2, 1)
plt.plot(x, np.sin(x))
plt.title("sin(x)")
plt.subplot(1, 2, 2)
plt.plot(x, np.cos(x))
plt.title("cos(x)")
plt.tight_layout()
plt.show()
x = np.linspace(0, 2*np.pi, 200)
plt.subplot(1, 2, 1)
plt.plot(x, np.sin(x))
plt.title("sin(x)")
plt.subplot(1, 2, 2)
plt.plot(x, np.cos(x))
plt.title("cos(x)")
plt.tight_layout()
plt.show()
LaTeX in Labels¶
In [17]:
Copied!
x = np.linspace(-5, 5, 400)
y = np.exp(-x**2)
plt.plot(x, y)
plt.xlabel("$x$")
plt.ylabel("$e^{-x^2}$")
plt.title("Gaussian Curve")
plt.show()
x = np.linspace(-5, 5, 400)
y = np.exp(-x**2)
plt.plot(x, y)
plt.xlabel("$x$")
plt.ylabel("$e^{-x^2}$")
plt.title("Gaussian Curve")
plt.show()
Semilog-y¶
In [18]:
Copied!
n = np.array([10, 20, 40, 80, 160])
error = np.exp(-0.3*n)
plt.semilogy(n, error, marker="o")
plt.xlabel("n")
plt.ylabel("Error")
plt.grid(True)
plt.show()
n = np.array([10, 20, 40, 80, 160])
error = np.exp(-0.3*n)
plt.semilogy(n, error, marker="o")
plt.xlabel("n")
plt.ylabel("Error")
plt.grid(True)
plt.show()
Log-Log Plot¶
In [19]:
Copied!
h = np.array([0.1, 0.05, 0.025, 0.0125])
error = h**2
plt.loglog(h, error, marker="o")
plt.xlabel("h")
plt.ylabel("Error")
plt.grid(True, which="both")
plt.show()
h = np.array([0.1, 0.05, 0.025, 0.0125])
error = h**2
plt.loglog(h, error, marker="o")
plt.xlabel("h")
plt.ylabel("Error")
plt.grid(True, which="both")
plt.show()
In [20]:
Copied!
order = np.log(error[:-1]/error[1:]) / np.log(h[:-1]/h[1:])
print(order)
order = np.log(error[:-1]/error[1:]) / np.log(h[:-1]/h[1:])
print(order)
[2. 2. 2.]
Contour Plot¶
In [21]:
Copied!
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X**2 + Y**2))
plt.contour(X, Y, Z, levels=20)
plt.colorbar()
plt.show()
x = np.linspace(-2, 2, 100)
y = np.linspace(-2, 2, 100)
X, Y = np.meshgrid(x, y)
Z = np.exp(-(X**2 + Y**2))
plt.contour(X, Y, Z, levels=20)
plt.colorbar()
plt.show()
Filled contour:¶
In [22]:
Copied!
plt.contourf(X, Y, Z, levels=20)
plt.colorbar()
plt.show()
plt.contourf(X, Y, Z, levels=20)
plt.colorbar()
plt.show()
Heatmap¶
In [23]:
Copied!
A = np.random.rand(20, 20)
plt.imshow(A, origin="lower")
plt.colorbar()
plt.show()
A = np.random.rand(20, 20)
plt.imshow(A, origin="lower")
plt.colorbar()
plt.show()
Read and display image¶
{python}
import matplotlib.pyplot as plt
img = plt.imread("image.jpg")
plt.imshow(img)
plt.axis("off")
plt.show()
3D Surface Plot¶
In [24]:
Copied!
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.plot_surface(X, Y, Z)
plt.show()
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
ax.plot_surface(X, Y, Z)
plt.show()
In [25]:
Copied!
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
surf = ax.plot_surface(X, Y, Z, cmap="viridis")
fig.colorbar(surf)
plt.show()
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
surf = ax.plot_surface(X, Y, Z, cmap="viridis")
fig.colorbar(surf)
plt.show()
In [26]:
Copied!
x = np.linspace(0, 1, 100)
y = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.pi*X)*np.sin(np.pi*Y)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
surf = ax.plot_surface(X, Y, Z, cmap="plasma")
fig.colorbar(surf)
plt.show()
x = np.linspace(0, 1, 100)
y = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, y)
Z = np.sin(np.pi*X)*np.sin(np.pi*Y)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
surf = ax.plot_surface(X, Y, Z, cmap="plasma")
fig.colorbar(surf)
plt.show()