Discover more from Daily Dose of Data Science
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
Here, you can create a plot of any desired layout by defining the plot structure as a string.
For instance, the string layout:
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.
👉 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.