Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I omit Matplotlib printed output in Python / Jupyter notebook?
When creating plots in Matplotlib within Jupyter notebooks, you often see unwanted printed output like [<matplotlib.lines.Line2D at 0x7faa3852fac8>]. This happens because Matplotlib functions return objects that Jupyter displays. Here are three effective methods to suppress this output.
Method 1: Using Semicolon
The simplest approach is adding a semicolon at the end of your plot command ?
import numpy as np import matplotlib.pyplot as plt x = np.linspace(1, 10, 1000) y = np.sin(x) # Without semicolon - shows output plt.plot(x, y)
[<matplotlib.lines.Line2D at 0x7faa3852fac8>]
import numpy as np import matplotlib.pyplot as plt x = np.linspace(1, 10, 1000) y = np.sin(x) # With semicolon - no output shown plt.plot(x, y); plt.show()
Method 2: Using Underscore Assignment
Assign the return value to an underscore variable to suppress the output ?
import numpy as np import matplotlib.pyplot as plt x = np.linspace(1, 10, 1000) y = np.cos(x) # Assign to underscore _ = plt.plot(x, y) plt.show()
Method 3: Using plt.show() Only
Call plt.show() immediately after plotting without storing the return value ?
import numpy as np import matplotlib.pyplot as plt x = np.linspace(1, 10, 1000) y = x**2 plt.plot(x, y) plt.show()
Comparison
| Method | Syntax | Best For |
|---|---|---|
| Semicolon | plt.plot(x, y); |
Quick suppression |
| Underscore | _ = plt.plot(x, y) |
When you might need the object later |
| plt.show() | plt.show() |
Explicit plot display |
Conclusion
The semicolon method is the quickest way to suppress Matplotlib output in Jupyter notebooks. Use underscore assignment when you might need the plot object reference later, or plt.show() for explicit control over plot display.
