Daily Dose of Data Science

Share this post

The Coolest Matplotlib Hack to Create Subplots Intuitively

www.blog.dailydoseofds.com

Discover more from Daily Dose of Data Science

High-quality insights on Data Science and Python, along with best practices — shared daily. Get a 550+ Page Data Science PDF Guide and 450+ Practice Questions Notebook, FREE.
Over 36,000 subscribers
Continue reading
Sign in

The Coolest Matplotlib Hack to Create Subplots Intuitively

One of the best Matplotlib hacks you will ever see.

Avi Chawla
May 29, 2023
27
Share this post

The Coolest Matplotlib Hack to Create Subplots Intuitively

www.blog.dailydoseofds.com
1
Share

This has to be the coolest thing I have ever learned about Matplotlib.

We mostly use plt.subplots() method to create subplots using Matplotlib.

But this, at times, gets pretty tedious and cumbersome. For instance:

  • it offers limited flexibility to create a custom layout.

  • it is prone to indexing errors, and more.

Instead, use the plt.subplot_mosaic() method.

Here, you can create a plot of any desired layout by defining the plot structure as a string.

For instance, the string layout:

  • AB

  • AC

will create three subplots, wherein:

  • subplot "A" will span the full first column

  • subplot "B" will span the top half of the second column

  • subplot "C" will span the bottom half of the second column

Next, create a subplot in a specific region by indexing the axes dictionary with its subplot key ("A", "B", or "C").

Isn't that super convenient and cool?

Read more: Matplotlib Docs.

Over to you: What are some cool Matplotlib hacks that you know of? Let me know :)

Thanks for reading Daily Dose of Data Science! Subscribe for free to learn something new and insightful about Python and Data Science every day. Also, get a Free Data Science PDF (250+ pages) with 200+ tips.


👉 Read what others are saying about this post on LinkedIn and Twitter.

👉 Tell the world what makes this newsletter special for you by leaving a review here :)

Review Daily Dose of Data Science

👉 If you liked this post, don’t forget to leave a like ❤️. It helps more people discover this newsletter on Substack and tells me that you appreciate reading these daily insights. The button is located towards the bottom of this email.

👉 If you love reading this newsletter, feel free to share it with friends!

Share Daily Dose of Data Science

👉 Sponsor the Daily Dose of Data Science Newsletter. More info here: Sponsorship details.


Find the code for my tips here: GitHub.

I like to explore, experiment and write about data science concepts and tools. You can read my articles on Medium. Also, you can connect with me on LinkedIn and Twitter.

27
Share this post

The Coolest Matplotlib Hack to Create Subplots Intuitively

www.blog.dailydoseofds.com
1
Share
Previous
Next
1 Comment
Share this discussion

The Coolest Matplotlib Hack to Create Subplots Intuitively

www.blog.dailydoseofds.com
Jean-Louis
May 29

Really neat! You're a TeamPlayer4Humanity.

Expand full comment
Reply
Share
Top
New
Community

No posts

Ready for more?

© 2023 Avi Chawla
Privacy ∙ Terms ∙ Collection notice
Start WritingGet the app
Substack is the home for great writing