Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase DPI of Matplotlib .show() in Jupyter Notebook

I'm using Matplotlib in a Jupyter Notebook to display an image of a map. The code looks like this:

%matplotlib inline  imgpath = './map.png'  import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np  from PIL import Image  img = Image.open(imgpath) print(img.size)  width, height = img.size # img.thumbnail((width * 2,height * 2), Image.ANTIALIAS)  # resizes image in-place imgplot = plt.imshow(img) plt.savefig('test.png', dpi = 300) 

The problem is, although the plt.savefig('test.png', dpi = 300) looks fine (because I changed the dpi to 300), the image displayed in the notebook is so low resolution I can't make anything out on it, and plt.imshow(img, dpi = 300) doesn't work:

Image displayed in notebook

So what I'm wondering is if there is a way to change the resolution of the image shown in the Jupyter Notebook?

like image 491
Alex S Avatar asked Aug 20 '18 19:08

Alex S


People also ask

How do I increase dpi in matplotlib?

Save Figure in High Resolution in Matplotlib We can plot figures in high resolutions by setting high values of dpi in matplotlib. pyplot. savefig() function. We can control the resolution of the saved figure through dpi parameter in savefig() function.

How do I make my matplotlib plot bigger in Jupyter?

Next, to increase the size of the plot in the jupyter notebook use plt. rcParams[“figure. figsize”] method and set width and height of the plot.

What is matplotlib dpi?

The dpi method of figure module of matplotlib library is the resolution in dots per inch. Syntax: fig.dpi. Parameters: This method does not accept any parameters. Returns: This method returns resolution in dots per inch.


2 Answers

Add this at the beginning of the notebook:

import matplotlib as mpl mpl.rcParams['figure.dpi'] = 300 

That's it !

like image 149
politinsa Avatar answered Oct 08 '22 03:10

politinsa


If your screen has Retina display, add the following line after %matplotlib inline (or somewhere else before plotting) in your notebook

%config InlineBackend.figure_format = 'retina' 

This will increase the display resolution of your plots within the Jupyter Notebook.

like image 23
Sheldore Avatar answered Oct 08 '22 04:10

Sheldore