Improve Matplotlib Plot Quality

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_formatsset_matplotlib_formats('svg')

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.

Also, the full archive I released recently has an entire section on data visualization:

Get the free PDF here: Daily Dose of Data Science Full Archive.

👉 Over to you: What are some other cool Matplotlib hacks that you are aware of?

Are you overwhelmed with the amount of information in ML/DS?

Every week, I publish no-fluff deep dives on topics that truly matter to your skills for ML/DS roles.

For instance:

Join below to unlock all full articles:

SPONSOR US

Get your product in front of 84,000 data scientists and other tech professionals.

Our newsletter puts your products and services directly in front of an audience that matters — thousands of leaders, senior data scientists, machine learning engineers, data analysts, etc., who have influence over significant tech decisions and big purchases.

To ensure your product reaches this influential audience, reserve your space here or reply to this email to ensure your product reaches this influential audience.

Reply

or to participate.