Matplotlib Basics

Python · Data Visualization

Theory

Matplotlib is Python's foundational plotting library; pandas' df.plot() and seaborn both draw with it underneath. Every chart is built from two main objects. The Figure is the whole canvas, and an Axes is one plotting area inside it with its own x-axis, y-axis, title and data. Everything you see, such as lines, bars and text, is an artist drawn on an Axes.

The recommended object-oriented style makes these explicit: fig, ax = plt.subplots() creates a Figure with one Axes, then ax.plot(x, y) draws, ax.set_title() and ax.set_xlabel() add text, and fig.savefig('sales.png') writes an image. The quicker pyplot style (plt.plot, plt.title) acts on the current Axes behind the scenes, which is handy for one-off charts but confusing once you have several. plt.show() displays the figure in a script; notebooks usually display it automatically.

Why we use it

A chart shows patterns, trends and outliers that a table of numbers hides. Understanding the Figure and Axes model lets you control every part of a chart and build multi-panel figures instead of fighting the defaults.

When to use it

Use matplotlib when you need full control, multi-panel layouts or report-ready images. For quick charts straight from a DataFrame use df.plot(); for statistical charts with less code use seaborn; for interactive web charts consider a library such as Plotly.

How it works

  1. import matplotlib.pyplot as plt
  2. fig, ax = plt.subplots() creates the canvas and one Axes
  3. Draw data: ax.plot / ax.bar / ax.scatter
  4. Add title, axis labels and legend
  5. Saving for a report? Yes: fig.savefig('chart.png', dpi=150). No: plt.show() to display.
  6. Chart rendered

Syntax

import matplotlib.pyplot as plt
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, y)
ax.set_title('...')  ax.set_xlabel('...')  ax.set_ylabel('...')
plt.show()

Example

fig, ax = plt.subplots()
ax.plot(['Jan', 'Feb', 'Mar'], [100, 250, 300])
ax.set_title('Monthly sales')
plt.show()

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic