› Forums › Python › MatPlotLib › Why Does plt.show() Display My Plot?
- This topic is empty.
-
AuthorPosts
-
March 2, 2026 at 9:19 pm #6160
Understanding How Matplotlib Handles Figures (Beginner to Intermediate Guide)
When learning Matplotlib, many beginners ask:
“If I used
ax.boxplot()to create the plot, why doesplt.show()display it?”
“What if there are multiple figures open?”
“How do I display only the latest plot?”Today, let’s break this down clearly.
📌 Step 1: What Is
plt?When we write:
import matplotlib.pyplot as pltpltis simply an alias for thematplotlib.pyplotmodule.The
pyplotmodule acts like a figure manager — it keeps track of all figures created during a session.
📊 Creating a Boxplot (Object-Oriented Style)
Example:
import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.boxplot(area_m2, vert=False) ax.set_xlabel("Area [sq meters]") ax.set_title("Distribution of Home Sizes") plt.show()Here’s what happens internally:
plt.subplots()creates:- A Figure object (
fig) - An Axes object (
ax)
- A Figure object (
ax.boxplot()draws on that figureplt.show()tells Matplotlib:
“Render all currently active figures.”
🔎 Important Concept: Active Figures
Matplotlib keeps every created figure in memory until you close it.
So if you previously ran:
fig1, ax1 = plt.subplots() ax1.plot([1, 2, 3])And then later:
fig2, ax2 = plt.subplots() ax2.plot([3, 2, 1]) plt.show()👉 Both figures will display.
Because both are still active.
⚠️ Why Does This Happen?
matplotlib.pyplotworks as a state machine.Internally, it maintains a list of open figures.
Whenplt.show()runs, it loops through that list and renders everything.
✅ How to Display Only the Latest Plot
🔹 Method 1 (Best Practice in Scripts)
Reset the environment before plotting:
plt.close('all')Then create your new figure:
fig, ax = plt.subplots() ax.boxplot(area_m2, vert=False) ax.set_xlabel("Area [sq meters]") ax.set_title("Distribution of Home Sizes") plt.show()Now only one figure exists — so only one displays.
🔹 Method 2 — Show Specific Figure
Since you stored the figure:
fig.show()This displays only that figure.
🔹 Method 3 — In Jupyter Notebooks
If the figure object is the last line:
figJupyter automatically renders just that figure.
🎨 Two Styles of Using Matplotlib
1️⃣ State-Based Style
plt.boxplot(area_m2) plt.show()2️⃣ Object-Oriented Style (Recommended)
fig, ax = plt.subplots() ax.boxplot(area_m2) plt.show()Even in object-oriented style,
plt.show()still works because pyplot manages the figures globally.
🧠 Mental Model (Very Important)
Think of it like this:
fig= Canvasax= Drawing areaax.boxplot()= Drawing on the canvasplt.show()= Hanging all canvases on the wall
If multiple canvases exist → all get displayed.
🎯 Key Takeaways
✔
pltis the alias formatplotlib.pyplot
✔plt.show()displays all open figures
✔ Matplotlib internally tracks active figures
✔ Useplt.close('all')to avoid showing old plots
✔ Object-oriented style is more scalable and professional
-
AuthorPosts
- You must be logged in to reply to this topic.

