5 Best Ways to Omit Matplotlib Printed Output in a Python Jupyter Notebook

πŸ’‘ Problem Formulation: When working in a Jupyter Notebook, running a cell that contains a Matplotlib plot often results in unwanted text output above the plot, typically information like <matplotlib.figure.Figure at 0x...>. In a professional context, we want our notebooks to be clean and only show the plots without this extra text clutter. Let’s explore how to suppress such output.

Method 1: Using the Semicolon

One of the simplest ways to prevent Matplotlib printouts is by adding a semicolon at the end of the plotting command. This is a shorthand method that tells the Jupyter notebook to suppress the output of the last line in the cell.

Here’s an example:

import matplotlib.pyplot as plt

plt.plot([0, 1, 2], [0, 1, 4]);

The output is the plot itself with no additional text output.

This code block imports Matplotlib and plots a simple line graph. By ending the plot command with a semicolon, we prevent the Jupyter Notebook from displaying the text output that normally accompanies the plot.

Method 2: Using plt.show()

The plt.show() function is designed to display all figures and block until the figures have been closed. When used at the end of plotting commands in a Jupyter Notebook cell, it can also prevent the display of the unwanted textual output.

Here’s an example:

import matplotlib.pyplot as plt

plt.plot([0, 1, 2], [0, 1, 4])
plt.show()

This generates a plot without any additional text output.

The plt.plot() command generates a plot while plt.show() ensures that the plot is displayed cleanly without any accompanying text output.

Method 3: Using IPython’s Display Function

IPython’s display function, when paired with clear_output, allows fine control over output and can effectively suppress unwanted text when plotting.

Here’s an example:

import matplotlib.pyplot as plt
from IPython.display import display, clear_output

plt.plot([0, 1, 2], [0, 1, 4])
clear_output(wait=True)
display(plt.gcf())

The plot appears without any additional text output.

The code generates a plot and then uses clear_output() to remove any previous output, followed by display() with plt.gcf() to present the current figure.

Method 4: Capturing the Output

By capturing the output of the plot command with the IPython’s %%capture magic command, you can prevent the display of any output, including textual information from Matplotlib.

Here’s an example:

%%capture capt
import matplotlib.pyplot as plt

plt.plot([0, 1, 2], [0, 1, 4])
plt.show()

No output is shown at all, and everything is captured in the capt object.

This approach uses one of the built-in ‘magic’ commands in Jupyter Notebook, %%capture, which suppresses all output from the cell and optionally stores it in a variable for later use.

Bonus One-Liner Method 5: Using the Matplotlib Inline Backend

By using the %matplotlib inline magic command, Matplotlib is configured to work with Jupyter Notebook, often eliminating extraneous textual output from plots.

Here’s an example:

%matplotlib inline
import matplotlib.pyplot as plt

plt.plot([0, 1, 2], [0, 1, 4])

The figure is displayed without any additional text output.

This one-liner instructs Jupyter to set up Matplotlib for use in the notebook environment, streamlining the plotting process to avoid unwanted text.

Summary/Discussion

  • Method 1: Semicolon. Simple and straightforward. May be unintuitive for those unfamiliar with the tactic.
  • Method 2: plt.show(). Official way to display plots. Ensures that the output is formatted correctly, but requires an additional function call.
  • Method 3: IPython’s Display Function. Offers fine control over what is displayed. Slightly more complex but very powerful.
  • Method 4: Capturing Output. Completely suppresses output and is useful for batch processing of plots. The downside is it may suppress outputs that you want to see.
  • Bonus Method 5: Matplotlib Inline Backend. Integrates Matplotlib more tightly with Jupyter. Doesn’t capture the output but usually helps in showing the plot without extra text.