visualization
Converted from
04_visualization.ipynbfor web reading.
Code cell 1
import numpy as np
import matplotlib.pyplot as plt
# Enable inline plotting
%matplotlib inline
plt.style.use('seaborn-v0_8-whitegrid')
Line Plot
Code cell 3
x = np.linspace(0, 2 * np.pi, 100)
y_sin = np.sin(x)
y_cos = np.cos(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y_sin, label='sin(x)', color='blue', linewidth=2)
plt.plot(x, y_cos, label='cos(x)', color='red', linewidth=2, linestyle='--')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Trigonometric Functions')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
Scatter Plot
Code cell 5
np.random.seed(42)
x = np.random.rand(50)
y = 2 * x + 0.5 + np.random.randn(50) * 0.2
plt.figure(figsize=(8, 6))
plt.scatter(x, y, c=x, cmap='viridis', s=100, alpha=0.7)
plt.colorbar(label='x value')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Scatter Plot with Color Mapping')
plt.show()
Bar Plot
Code cell 7
categories = ['A', 'B', 'C', 'D', 'E']
values = [23, 45, 56, 78, 32]
plt.figure(figsize=(8, 6))
bars = plt.bar(categories, values, color='steelblue', edgecolor='black')
plt.bar_label(bars)
plt.xlabel('Category')
plt.ylabel('Value')
plt.title('Bar Chart')
plt.show()
Histogram
Code cell 9
data = np.random.randn(1000)
plt.figure(figsize=(8, 6))
plt.hist(data, bins=30, color='green', alpha=0.7, edgecolor='black')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Histogram of Normal Distribution')
plt.axvline(data.mean(), color='red', linestyle='--', label=f'Mean: {data.mean():.2f}')
plt.legend()
plt.show()
Subplots
Code cell 11
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
# Plot 1: Line
x = np.linspace(0, 10, 100)
axes[0, 0].plot(x, np.sin(x))
axes[0, 0].set_title('Line Plot')
# Plot 2: Scatter
axes[0, 1].scatter(np.random.rand(30), np.random.rand(30))
axes[0, 1].set_title('Scatter Plot')
# Plot 3: Bar
axes[1, 0].bar(['A', 'B', 'C'], [10, 20, 15])
axes[1, 0].set_title('Bar Plot')
# Plot 4: Pie
axes[1, 1].pie([30, 20, 25, 25], labels=['A', 'B', 'C', 'D'], autopct='%1.1f%%')
axes[1, 1].set_title('Pie Chart')
plt.tight_layout()
plt.show()
Heatmap
Code cell 13
# Create correlation-like data
np.random.seed(42)
data = np.random.rand(5, 5)
plt.figure(figsize=(8, 6))
im = plt.imshow(data, cmap='coolwarm')
plt.colorbar(im)
plt.xticks(range(5), ['A', 'B', 'C', 'D', 'E'])
plt.yticks(range(5), ['A', 'B', 'C', 'D', 'E'])
plt.title('Heatmap')
# Add text annotations
for i in range(5):
for j in range(5):
plt.text(j, i, f'{data[i, j]:.2f}', ha='center', va='center', color='black')
plt.show()