Matplotlib Deep Dive
108 words
1 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Matplotlib Deep Dive ## Matplotlib Best Practices - Use `plt.subplots()` (the OO API) instead of `plt.plot()` (pyplot) - Always include axis labels and titles - Use `fig.tight_layout()` to prevent overlapping - Set appropriate figure sizes - Export to SVG/PDF for vector graphics in publications [Join Discord](http...

Matplotlib Deep Dive
pythonimport matplotlib.pyplot as plt import numpy as np # Create figure and axes fig, axes = plt.subplots(2, 2, figsize=(10, 8)) # Custom styling plt.style.use('seaborn-v0_8-whitegrid') # Annotations x = np.linspace(0, 10, 100) axes[0, 0].plot(x, np.sin(x), label='sin(x)') axes[0, 0].annotate('Peak', xy=(np.pi/2, 1), xytext=(np.pi/2, 1.2), arrowprops=dict(arrowstyle='->')) axes[0, 0].legend() axes[0, 0].set_title('Sine Wave') axes[0, 0].set_xlabel('x') axes[0, 0].set_ylabel('sin(x)') # Tight layout plt.tight_layout() plt.show()
Matplotlib Best Practices
- Use
plt.subplots()(the OO API) instead ofplt.plot()(pyplot) - Always include axis labels and titles
- Use
fig.tight_layout()to prevent overlapping - Set appropriate figure sizes
- Export to SVG/PDF for vector graphics in publications Join Discord PreviousAccessible DesignNextTool Comparison