Subplots and Layout

Python · Data Visualization

Theory

plt.subplots(nrows, ncols) creates a grid of Axes in one Figure and returns them as a NumPy array. fig, axes = plt.subplots(2, 2) gives axes with shape (2, 2): axes[0, 0] is top-left and axes[1, 1] bottom-right. With one row, plt.subplots(1, 3) returns a 1-D array, so you use axes[0], axes[1]. Looping over axes.flat visits every panel in turn.

figsize=(10, 6) sets the canvas size in inches. sharex=True or sharey=True makes panels use the same axis range, which keeps comparisons honest. fig.suptitle() adds one title for the whole figure. Labels of neighbouring panels often overlap, so call fig.tight_layout() at the end or create the figure with constrained_layout=True to space things automatically. Unused panels can be hidden with ax.axis('off').

Why we use it

Related charts are easier to compare side by side than in separate images. Subplots put a small dashboard of views (trend, distribution, breakdown) into one figure with consistent sizes and axes.

When to use it

Use subplots to compare the same chart across groups (small multiples) or to show different views of one dataset. Share axes when panels show the same measure. If panels have nothing in common, separate figures are clearer.

How it works

  1. Several charts to show together
  2. fig, axes = plt.subplots(rows, cols, figsize=...)
  3. Do panels show the same measure? Yes: Add sharex / sharey. No: Keep independent axes.
  4. Draw on each panel: axes[r, c] or loop over axes.flat
  5. fig.suptitle() and fig.tight_layout()
  6. One figure with a grid of charts

Syntax

fig, axes = plt.subplots(2, 2, figsize=(10, 6), sharey=True)
axes[0, 1].plot(x, y)
for ax in axes.flat: ...
fig.suptitle('...')
fig.tight_layout()

Example

fig, axes = plt.subplots(1, 2, figsize=(10, 4))
axes[0].plot([1, 2, 3])
axes[1].bar(['A', 'B'], [3, 5])
fig.tight_layout()

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