How to make your Matplotlib plot display correctly using Python

7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Most of the time, your plot won't display correctly.

This is because you add stuff on them and the chart ends up needing to be rearranged.

Matplotlib provides us with a clean way to do exactly that.

The .tight_layout() method.

It will try to fit all your plots nicely in the figure.

Here is what a badly fitted plot can look like
Here is what we aim for

Here is the code

# To plot it
import matplotlib.pyplot as plt

# We generate our data
x = range(10)

# We create our canvas
fig, axes = plt.subplots(2,
                         2,
                         figsize=(8,6))

# We plot a line plot
axes[0][0].plot(x, x)
axes[0][0].set_title("plot 1")
axes[0][0].set_xlabel("x labels")
axes[0][0].set_ylabel("x labels")

# We plot our second plot onto the figure
axes[0][1].plot(x, x)
axes[0][1].set_title("plot 2")
axes[0][1].set_xlabel("x labels")
axes[0][1].set_ylabel("x labels")

# We plot our third plot onto the figure
axes[1][0].plot(x, x)
axes[1][0].set_title("plot 3")
axes[1][0].set_xlabel("x labels")
axes[1][0].set_ylabel("x labels")

# We plot our fourth plot onto the figure
axes[1][1].plot(x, x)
axes[1][1].set_title("plot 4")
axes[1][1].set_xlabel("x labels")
axes[1][1].set_ylabel("x labels")

# Here we tidy up the figure
fig.tight_layout()

# We show the plot
plt.savefig("test_2.jpg")

As you can see, the plot is well fitted to the figure which is what we were aiming for.

Here you are! You now know how to make your plot display correctly with Matplotlib using Python.

More on Matplotlib

If you like what you've just read and want to know more about the Matplotlib library (e.g. how to add labels, plot different types of plots, etc...) check out the other articles I wrote on the topic, just here :

Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.