Create compelling visualizations for data analysis
Matplotlib is Python's most popular plotting library. It provides complete control over every aspect of a figure and works seamlessly with NumPy and Pandas.
Create your first plots:
import matplotlib.pyplot as plt
import numpy as np
# Line plot
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y1, label='sin(x)', linewidth=2)
plt.plot(x, y2, label='cos(x)', linewidth=2, linestyle='--')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Trigonometric Functions')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
# Scatter plot
np.random.seed(42)
x_scatter = np.random.randn(100)
y_scatter = 2 * x_scatter + np.random.randn(100) * 0.5
plt.figure(figsize=(8, 6))
plt.scatter(x_scatter, y_scatter, alpha=0.6, edgecolors='k')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Scatter Plot Example')
plt.show()
print("Plots created successfully!")[Plot displays showing sine and cosine waves] [Scatter plot showing linear relationship with noise] Plots created successfully!
Create complex multi-panel figures:
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
# Plot 1: Line plot
axes[0, 0].plot(x, np.exp(-x/10)*np.sin(x))
axes[0, 0].set_title('Damped Sine Wave')
axes[0, 0].grid(True)
# Plot 2: Bar chart
categories = ['A', 'B', 'C', 'D']
values = [23, 45, 56, 78]
axes[0, 1].bar(categories, values, color='steelblue')
axes[0, 1].set_title('Bar Chart')
# Plot 3: Histogram
data = np.random.randn(1000)
axes[1, 0].hist(data, bins=30, edgecolor='black', alpha=0.7)
axes[1, 0].set_title('Histogram')
# Plot 4: Pie chart
sizes = [30, 25, 20, 25]
axes[1, 1].pie(sizes, labels=categories, autopct='%1.1f%%')
axes[1, 1].set_title('Pie Chart')
plt.tight_layout()
plt.show()[Multi-panel figure displaying 4 different plot types]