PYTHON FOR DATA SCIENCE: FROM ARRAYS TO ANALYSIS / L06MATPLOTLIB FUNDAMENTALS: CREATING PUBLICATION-QUALITY PLOTS
课程 · 10 · 06 / 10
LESSON 06 · INTERMEDIATE · 60 MIN · ◆ 2 INSTRUMENTS

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.

TIP

Learning 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.

FIG. 02Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 02Interactive Python code execution environment

Basic Plotting

Line Plots

FIG. 04Graph Plotter
INTERACTIVE
LOADING INSTRUMENT
Fig. 04Interactive plotting tool for visualizing data and relationships
FIG. 06Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 06Interactive Python code execution environment

Scatter Plots

FIG. 08Graph Plotter
INTERACTIVE
LOADING INSTRUMENT
Fig. 08Interactive plotting tool for visualizing data and relationships
FIG. 10Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 10Interactive Python code execution environment

Bar Charts

FIG. 12Graph Plotter
INTERACTIVE
LOADING INSTRUMENT
Fig. 12Interactive plotting tool for visualizing data and relationships
FIG. 14Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 14Interactive Python code execution environment

Histograms

FIG. 16Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 16Interactive Python code execution environment

Pie Charts

FIG. 18Graph Plotter
INTERACTIVE
LOADING INSTRUMENT
Fig. 18Interactive plotting tool for visualizing data and relationships
FIG. 20Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 20Interactive Python code execution environment

Customizing Plots

Colors, Markers, and Line Styles

FIG. 22Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 22Interactive Python code execution environment

Titles, Labels, and Legends

FIG. 24Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 24Interactive Python code execution environment

Axis Customization

FIG. 26Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 26Interactive Python code execution environment

Subplots and Figure Layout

Multiple Plots in One Figure

FIG. 28Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 28Interactive Python code execution environment

Sharing Axes

FIG. 30Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 30Interactive Python code execution environment

Saving Figures

FIG. 32Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 32Interactive Python code execution environment

Object-Oriented Interface

The object-oriented approach gives you more control:

FIG. 34Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 34Interactive Python code execution environment

Practical Example: Complete Dashboard

FIG. 36Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 36Interactive Python code execution environment

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

LibraryRelationship
seabornHigh-level API built on matplotlib
pandasdf.plot() uses matplotlib
plotlyAlternative (interactive)
bokehAlternative (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

FIG. 38Python Code Executor
INTERACTIVE
LOADING INSTRUMENT
Fig. 38Interactive Python code execution environment

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)

Official Docs

Modern Best Practices

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.