课程 · 10 · 06 / 10
Matplotlib Fundamentals: Creating Publication-Quality Plots
Master the fundamentals of data visualization with matplotlib. Learn to create, customize, and combine various plot types for effective data communication.
TIPLearning Objectives: After this lesson, you'll master the fundamentals of data visualization with matplotlib—creating, customizing, and combining various plot types for effective data communication.
Why Matplotlib?
Matplotlib is the grandfather of Python visualization libraries. While newer libraries like seaborn and plotly offer convenience, understanding matplotlib gives you complete control over every pixel of your plots.
Basic Plotting
Line Plots
Scatter Plots
Bar Charts
Histograms
Pie Charts
Customizing Plots
Colors, Markers, and Line Styles
Titles, Labels, and Legends
Axis Customization
Subplots and Figure Layout
Multiple Plots in One Figure
Sharing Axes
Saving Figures
Object-Oriented Interface
The object-oriented approach gives you more control:
Practical Example: Complete Dashboard
Key Takeaways
✅ Basic plots: plt.plot(), plt.scatter(), plt.bar(), plt.hist(), plt.pie()
✅ Customization: Colors, markers, line styles, labels, titles, legends
✅ Subplots: Use plt.subplots() for multiple plots in one figure
✅ Object-oriented: fig, ax = plt.subplots() gives more control
✅ Saving: plt.savefig() with DPI and format options
✅ Layout: plt.tight_layout() prevents overlapping elements
Connections: Matplotlib in the Ecosystem
🔗 Connection to Other Libraries
| Library | Relationship |
|---|---|
| seaborn | High-level API built on matplotlib |
| pandas | df.plot() uses matplotlib |
| plotly | Alternative (interactive) |
| bokeh | Alternative (web-focused) |
🔗 Connection to Data Science
Visualization is essential for:
- EDA: Understanding data distributions
- Communication: Presenting findings
- Model evaluation: Learning curves, confusion matrices
- Publication: Journal-quality figures
Practice Exercises
Exercise 1: Multi-Plot Figure
Next Steps
In the next lesson, we'll explore seaborn—a high-level library that creates beautiful statistical visualizations with minimal code.
Ready for easier, more beautiful plots? Seaborn is next!
Further Reading
Visual Galleries (Steal Code from These)
- Matplotlib Gallery — every plot type with copy-pasteable source. The single most useful matplotlib resource.
- Python Graph Gallery — chart-type-first organization across matplotlib + seaborn + plotly.
- The Data Visualisation Catalogue — pick a chart type by what you're trying to communicate, then see the matplotlib equivalent.
Official Docs
- Matplotlib — Quick Start Guide — the official walkthrough.
- Matplotlib —
pyplotvs Object-Oriented API — once you outgrowplt.plot, learn the OOfig, ax = plt.subplots()pattern. - Matplotlib — Styling and Themes —
rcParams, style sheets, themes.
Modern Best Practices
- Effective matplotlib (Practical Business Python) — Chris Moffitt. The post that taught everyone the OO API.
- Matplotlib Cyberpunk style — drop-in dark theme with neon glow effects. Surprisingly delightful.
matplotx— modern theme set;matplotx.styles.dracula,nord,aura.
Beyond matplotlib
- Plotly Python — interactive, zoomable, exportable to standalone HTML.
- Altair — declarative grammar of graphics. Concise, principled, JSON-spec under the hood.
- Bokeh — for huge interactive dashboards.
Books
- Book: Python Data Science Handbook — Chapter 4. Free online.
- Book: Storytelling with Data — Cole Nussbaumer Knaflic. The why of visualization; transformative, library-agnostic.