0
0
MatplotlibHow-ToBeginner ยท 3 min read

How to Use plt.show in Matplotlib for Plot Display

Use plt.show() in matplotlib to display all open figure windows and render your plots on the screen. It should be called after creating your plot commands to make the plot visible.
๐Ÿ“

Syntax

The basic syntax of plt.show() is simple and requires no arguments. It is called as a function to display the current figure or all open figures.

  • plt: The common alias for the matplotlib.pyplot module.
  • show(): Function that renders the plot window.
python
import matplotlib.pyplot as plt

plt.show()
Output
A blank plot window opens if no plot is created; otherwise, it shows the current plot.
๐Ÿ’ป

Example

This example creates a simple line plot and then uses plt.show() to display it. Without plt.show(), the plot may not appear in some environments.

python
import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 20, 25, 30]

plt.plot(x, y)
plt.title('Simple Line Plot')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.show()
Output
A window opens displaying a line graph with points (1,10), (2,20), (3,25), (4,30) and labeled axes and title.
โš ๏ธ

Common Pitfalls

One common mistake is forgetting to call plt.show(), which can cause plots not to appear, especially in scripts or some IDEs. Another issue is calling plt.show() multiple times unnecessarily, which can slow down your program or open multiple windows.

Also, in interactive environments like Jupyter notebooks, plots often display automatically, so plt.show() is optional but recommended for clarity.

python
import matplotlib.pyplot as plt

# Wrong: Plot created but not shown
plt.plot([1, 2, 3], [4, 5, 6])
# Missing plt.show() means no plot window in some setups

# Right: Plot created and shown
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
Output
The first plot does not appear in some environments; the second plot appears correctly.
๐Ÿ“Š

Quick Reference

Tips for using plt.show():

  • Always call plt.show() once after all plotting commands to display the figure.
  • In scripts, it is necessary to see the plot window.
  • In interactive notebooks, it is optional but good practice.
  • Calling plt.show() clears the current figure by default.
โœ…

Key Takeaways

Call plt.show() after creating your plot to display it on screen.
Without plt.show(), plots may not appear in scripts or some IDEs.
In Jupyter notebooks, plt.show() is optional but recommended for clarity.
Avoid calling plt.show() multiple times unnecessarily.
plt.show() clears the current figure after displaying it.