DATA SCIENCE /// MATPLOTLIB BASICS /// FIGURES & AXES /// SCATTER PLOTS /// DATA SCIENCE /// MATPLOTLIB BASICS /// FIGURES & AXES /// SCATTER PLOTS ///

Matplotlib Basics

Convert raw numbers into actionable intelligence. Master Python's standard Data Visualization library.

main.py
1 / 10
12345
📊

>>>Data isn't useful until we can see it. Matplotlib is the bedrock of Python Data Visualization. Let's learn the Object-Oriented interface.


Architecture

COMPILE DEPENDENCIES TO UNLOCK.

Concept: Figure & Axes

Always decouple the physical window (Figure) from the charting area (Axes) for robust OOP code.

Syntax Check

Which line correctly creates a Figure and an Axes object simultaneously?


Data Science Syndicate

Peer Review Notebooks

LIVE

Built an impressive EDA dashboard? Share your Jupyter notebooks and get feedback from our data science community.

Matplotlib Basics: Painting with Data

Author

Pascual Vila

AI & Data Science Architect // Code Syllabus

Data without visualization is just a spreadsheet. Matplotlib empowers data scientists to uncover hidden patterns, convey complex metrics, and generate publication-ready plots using Python.

Two Paradigms: Pyplot vs Object-Oriented

Matplotlib offers two approaches to creating charts. The Pyplot (State-based) approach uses commands like plt.plot() and plt.title(). It's quick for simple scripts but messy for complex layouts because it implicitly assumes you want to draw on the "current" active figure.

The gold standard for data science is the Object-Oriented (OO) interface. Here, you explicitly instantiate two objects: the Figure (the blank canvas) and the Axes (the chart box containing the data).

Anatomy of a Plot

  • Figure (fig): The top-level container holding all plot elements. You can set the size using figsize=(10, 5).
  • Axes (ax): The actual bounding box where your data appears. A Figure can hold multiple Axes (Subplots).
  • Axis: The number-line-like objects (X-axis, Y-axis) that set limits and generate ticks.

Essential Plot Types

Depending on your Exploratory Data Analysis (EDA) goals, you invoke different methods on your ax object:

  • ax.plot(x, y) - Line graphs. Best for tracking trends over time (Time Series).
  • ax.scatter(x, y) - Scatter plots. Best for identifying correlations between numerical variables.
  • ax.bar(x, height) - Bar charts. Best for comparing categorical variables.
View Subplot Implementation Tips+

Handling multiple charts: When you use fig, axes = plt.subplots(nrows=2, ncols=2), the axes variable becomes a 2D Numpy Array. You must index it to draw on specific charts: axes[0, 1].plot(x, y) draws on the top-right chart. Always use plt.tight_layout() before displaying to prevent labels from overlapping!

🤖 AI Query Database

What is the best data visualization library in Python?

Matplotlib is the foundational library for Python data visualization. While libraries like Seaborn (which is built on top of Matplotlib) provide better default aesthetics for statistical plots, Matplotlib offers maximum customizability. For interactive web dashboards, Plotly or Bokeh are often preferred, but Matplotlib remains the absolute standard for static, publication-ready Data Science EDA.

Why does my Matplotlib graph not show when running a Python script?

If you are not using a Jupyter Notebook environment, Matplotlib requires an explicit command to render the window to the operating system display. You must call plt.show() at the very end of your script to block execution and open the graphical interface. In Jupyter, the `%matplotlib inline` magic command handles this automatically.

How do I save a Matplotlib chart as an image (PNG/PDF)?

Instead of taking a screenshot, you can export high-resolution assets directly using the Figure object's method. Call fig.savefig('my_plot.png', dpi=300, bbox_inches='tight'). Note: Ensure you call savefig before calling plt.show(), otherwise it will save a blank canvas!

API Glossary

plt.subplots()
Returns a tuple containing a Figure object and an array of Axes objects. Essential for OO layout.
reference.py
ax.plot()
Plots y versus x as lines and/or markers. The workhorse for line charts and time series.
reference.py
ax.scatter()
A scatter plot of y vs x with varying marker size and/or color. Crucial for correlation analysis.
reference.py
ax.set_title()
Sets the title for the specific axes (subplot). Replaces the state-based plt.title().
reference.py
ax.legend()
Places a legend on the axes using the 'label' parameters defined in your plotting functions.
reference.py
plt.show()
Starts an event loop, looks for all currently active figure objects, and opens interactive windows displaying them.
reference.py