Introduction
Matplotlib is the foundational plotting library for Python data science. It provides comprehensive control over visualizations and serves as the backbone for many other visualization libraries.
Why Matplotlib?
- Full control: Customize every aspect of plots
- Wide support: Bar charts, line plots, scatter plots, 3D plots, and more
- Integration: Works seamlessly with NumPy and Pandas
Basic Plot
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'b-', linewidth=2)
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.title('Sine Wave')
plt.grid(True)
plt.show()
Multiple Plots
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
axes[0, 0].plot(x, np.sin(x))
axes[0, 1].plot(x, np.cos(x))
axes[1, 0].plot(x, np.tan(x))
axes[1, 1].plot(x, x**2)
plt.tight_layout()
plt.show()
Plot Types
# Bar chart
plt.bar(['A', 'B', 'C'], [10, 20, 15])
# Histogram
data = np.random.randn(1000)
plt.hist(data, bins=30, alpha=0.7)
# Scatter plot
plt.scatter(x, y, c=y, cmap='viridis')
# Pie chart
plt.pie([25, 35, 40], labels=['A', 'B', 'C'])
Styling and Customization
plt.style.use('seaborn-v0_8-darkgrid')
plt.plot(x, y, color='#FF5733', linestyle='--',
marker='o', markersize=8, label='Data')
plt.legend(loc='upper right')
plt.xlim(0, 10)
plt.ylim(-1.5, 1.5)
Saving Figures
plt.savefig('plot.png', dpi=300, bbox_inches='tight')
plt.savefig('plot.pdf')
plt.savefig('plot.svg')
Key Takeaways
- Matplotlib provides fine-grained control over visualizations
- Use subplots for multi-panel figures
- Save in multiple formats for different use cases