Significantly Improve the Quality of Matplotlib Plots by Doing (Almost) Nothing
A hidden Matplotlib hack waiting to be discovered.
Matplotlib has always been my go-to library for data visualizations.
But one of the things I dislike about using Matplotlib in Jupyter Notebook is that its plots become quite dull and blurry when scaled/zoomed.
This is evident from the image below:
This happens because Matplotlib plots are rendered as an image by default. Thus, any scaling/zooming drastically distorts their quality.
If you frequently use Matplotlib in Jupyter Notebook, then I am sure you would have noticed this too.
Yet, here’s a pretty cool hack to improve their quality significantly.
Instead of the default image rendering, start rendering your plots as a scalable vector graphic (SVG).
As the name suggests, it can be scaled without compromising the plot’s quality, which is evident from the image below:
As demonstrated in the image above, the plot rendered as SVG clearly outshines and is noticeably sharper than the default plot, despite zooming.
The following code lets you change the render format to SVG.
from matplotlib_inline.backend_inline import set_matplotlib_formats
Alternatively, you can also use the following code:
%config InlineBackend.figure_format = 'svg'
If the difference is not apparent in the image above, I recommend trying it and noticing it.
Download this Jupyter Notebook to test this: Matplotlib SVG Notebook.
👉 Over to you: What are some other cool Matplotlib hacks that you are aware of?
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 (550+ pages) with 320+ tips.
👉 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.
Thanks for reading!
Latest full articles
If you’re not a full subscriber, here’s what you missed last month:
To receive all full articles and support the Daily Dose of Data Science, consider subscribing:
👉 Tell the world what makes this newsletter special for you by leaving a review here :)
👉 If you love reading this newsletter, feel free to share it with friends!