Matplotlib Inline in Python

When I first started working with Python for data visualization, I often wondered about the term Matplotlib inline. It’s a phrase that pops up frequently, especially when working with Jupyter notebooks. But what does it mean? And how can you use it effectively?

In this article, I’ll share my firsthand experience with Matplotlib inline, explain what it is, why it matters, and how you can run Matplotlib plots inline.

Let’s get in!

What is Matplotlib Inline in Python?

Matplotlib is a useful plotting library in Python that helps you create static, animated, and interactive visualizations. When you run Python code that generates plots, the output usually appears in a separate window or pop-up.

However, in environments like Jupyter notebooks, you often want your plots to appear inline, right inside the notebook cells, so you can see the visualizations immediately below your code. This behavior is enabled by the %matplotlib inline magic command.

Using %matplotlib inline tells Jupyter to display plots directly in the notebook, making it easier to analyze and share your results without switching windows.

Read Matplotlib Not Showing Plot

How to Use Matplotlib Inline

VS Code is a popular editor among Python developers. While %matplotlib inline works naturally in Jupyter notebooks, you might wonder how to get similar inline behavior in VS Code, especially if you’re not using notebooks.

Here are a few methods I’ve used:

Check out Matplotlib Multiple Plots

Method 1: Use the VS Code Jupyter Extension

If you prefer working with notebooks, VS Code supports Jupyter notebooks natively through its extension.

  • Install the Jupyter extension in VS Code.
  • Open or create a .ipynb notebook file.
  • Use %matplotlib inline at the top of your notebook cells.

You can check the screenshot below to get more clarity.

matplotlib inline

Method 2: Use Python Interactive Window in VS Code

VS Code offers a Python Interactive window, which behaves like a Jupyter notebook but within your Python script environment.

  • Open your Python script (.py file).
  • Add the line %matplotlib inline at the top (this works in Interactive mode).
  • Highlight your code or specific cells and press Shift + Enter to run in the interactive window.
  • Your plots will appear inline inside the interactive window.

This method is handy if you want to mix script editing with interactive plotting.

Method 3: Display Plots Inline Using plt.show()

If you’re running Python scripts normally in VS Code’s terminal, plots usually open in a separate window. While this isn’t inline plotting, it’s a classic way to visualize plots.

Here’s a simple example:

import matplotlib.pyplot as plt

# Sample data: Average monthly temperatures in New York City (°F)
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
temperatures = [32, 35, 42, 54, 64, 73, 79, 78, 70, 59, 48, 37]

plt.plot(months, temperatures, marker='o')
plt.title('Average Monthly Temperatures in NYC')
plt.xlabel('Month')
plt.ylabel('Temperature (°F)')
plt.grid(True)
plt.show()

You can see the output in the screenshot below.

%matplotlib inline

Running this script in VS Code will open a separate window displaying the plot. This is useful when you want a larger view or are running scripts outside notebook environments.

Read Matplotlib Legend Font Size

What if You Don’t Use %matplotlib inline?

In Jupyter notebooks, if you skip %matplotlib inline plots might not display automatically after your plotting commands. You’ll need to call plt.show() explicitly to see the graph.

In VS Code scripts, plt.show() is essential to render the plot window. This is important to remember because forgetting it often leads to confusion when no plot appears.

Summary

  • Matplotlib inline is a magic command used mainly in Jupyter notebooks to display plots directly inside the notebook.
  • Using %matplotlib inline enhances your workflow by showing immediate visual feedback.
  • In VS Code, you can get inline plotting by using the Jupyter extension or the Python Interactive window.
  • For regular Python scripts in VS Code, use plt.show() to display plots in a separate window.
  • Understanding these methods helps you choose the best workflow for your data visualization tasks.

Matplotlib is an essential tool in any Python developer’s toolkit, especially when working with data. Knowing how to display plots inline or in separate windows, depending on your environment, can save you time and make your analysis more effective.

If you’re working with data in VS Code and want that seamless inline plotting experience, try the Jupyter extension or the Python Interactive window. Both offer the power of inline plots without leaving your favorite editor.

You may like to read other articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.