A Simple Trick That Significantly Improves The Quality of Matplotlib Plots

A simple one-liner to go from dull to sharp plots.

Matplotlib plots often appear dull and blurry, especially when scaled or zoomed. Yet, here's a simple trick to significantly improve their quality.

Matplotlib plots are rendered as an image by default. Thus, any scaling/zooming drastically distorts their quality.

Instead, always render your plot as a scalable vector graphic (SVG). As the name suggests, they can be scaled without compromising the plot's quality.

As demonstrated in the image above, the plot rendered as SVG clearly outshines and is noticeably sharper than the default plot.

The following code lets you change the render format to SVG. If the difference is not apparent in the image above, I would recommend trying it yourself and noticing the difference.

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'

P.S. If there’s a chance that you don’t know what is being depicted in the bar plot above, check out this YouTube video by Numberphile.

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

👉 Tell me you liked this post by leaving a heart react ❤️.

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

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.

Reply

or to participate.