matplotlib
The foundational library for creating static, animated, and interactive visualizations in Python. Highly customizable and the industry standard for publication-quality figures. Use for 2D plotting, scientific data visualization, heatmaps, contours, vector fields, multi-panel figures, LaTeX-formatted
By tondevrel · 361 installs
npx skills add tondevrel/scientific-agent-skills --skill matplotlib
Source repository · Upstream listing
Matplotlib Data Visualization
The most widely used library for 2D (and basic 3D) plotting. It provides full control over every element of a figure, from line styles to axis spines.
When to Use
Creating publication quality 2D plots (Line, Scatter, Bar, Hist)
Visualizing scientific data (Heatmaps, Contours, Vector fields)
Generating complex multi panel figures
Fine tuning plots for papers/reports (LaTeX support)
Building custom visualization tools and dashboards
Plotting data directly from NumPy arrays or Pandas DataFrames
Reference Documentation
Official docs : https://matplotlib.org/stable/index.html
Gallery : https://matplotlib.org/stable/gallery/index.html (Essential for finding examples)
Search patterns : plt.subplots , ax.set title , ax.legend , plt.savefig , matplotlib.colors
Core Principles
Two Interfaces: Choose Wisely
Interface Method Use Case
Object Oriented (OO) fig, ax = plt.subplots() Recommended. Best for complex, reproducible plots.
Pyplot (State based) plt.plot(x, y) Quick interactive checks. Avoid for scripts/modules.
Use Matplotlib For
High level control over figure layout.
Precise styling for publication.
Embedding plots in GUI applications.
Do NOT Use For
Interactive web dashboards (use Plotly or Bokeh).
Rapid statistical exploration (use Seaborn — it's built on Matplotlib but simpler for stats).
Very large datasets ( 1M points) in real time (use Datashader or VisPy).
Quick Reference
Installation
Standard Imports
Basic Pattern The OO Interface (The "Proper" Way)
Critical Rules
✅ DO
Use the OO interface ( ax.method() ) It prevents errors in multi plot scripts.
Use bbox inches='tight' When saving, to ensure labels aren't cut off.
Set dpi Use 300+ for print, 72 100 for web.
Close figures Use plt.close('all') in loops to avoid memory leaks.
Label everything Every axis must have a label and units.
Vector formats Save as .pdf or .svg for academic papers (lossless scaling).
Colorblind friendly Use tab10 or viridis colormaps.
❌ DON'T
Mix plt. and ax. It leads to "hidden state" bugs.
Use plt.show() in loops It blocks execution; use fig.savefig() instead.
Manual legend placement Let ax.legend(loc='best') try first.
Hardcode font sizes Use plt.rcParams.update({'font.size': 12}) for consistency.
Use "Rainbow" (Jet) It creates false gradients; use perceptually uniform maps like magma or inferno .
Anti Patterns (NEVER)
Anatomy of a Plot
Labels, Ticks, and Styles
Advanced Layouts
Subplots and GridSpec
Scientific Plot Types
Heatmaps and Colorbars
Histograms and Error Bars
3D Plotting
Formatting for Publication
Using LaTeX and RcParams
Practical Workflows
1. Multi dataset Comparison Workflow
2. Monitoring Real time Data (Interactive)
3. Creating a Cluster Map / Correlation Matrix
Performance Optimization
Plotting Large Data
Common Pitfalls and Solutions
Date/Time Axis issues
Multiple Legends on one plot
Image Saving Quality (Clipping)
Best Practices
1. Always use the OO interface ( fig, ax = plt.subplots() ) for scripts and modules
2. Save figures with appropriate formats Use PDF/SVG for publications, PNG for web
3. Set DPI appropriately 300+ for print, 72 100 for screen
4. Use bbox inches='tight' when saving to prevent clipping
5. Close figures in loops to prevent memory leaks
6. Use colorblind friendly colormaps Avoid 'jet', prefer 'viridis', 'plasma', 'inferno'
7. Label all axes with descriptive names and units
8. Use constrained layout=True for subplots to prevent overlap
9. Configure global styles with plt.rcParams for consistency
10. Test plots at target resolution before finalizing