The Coolest Matplotlib Hack to Create Subplots Intuitively
One of the best Matplotlib hacks you will ever see.
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 :)
👉 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 :)
👉 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!
👉 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.
Really neat! You're a TeamPlayer4Humanity.