Increase DPI of Matplotlib .show() in Jupyter Notebook

荒凉一梦 提交于 2019-12-18 12:27:13

问题


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:

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

Thanks a lot, Alex

Answer:

Politinsa suggested I add this to the beginning of the file:

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

And it fixes the issue.


回答1:


Add this at the beginning of the notebook:

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

That's it !




回答2:


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.



来源:https://stackoverflow.com/questions/51937381/increase-dpi-of-matplotlib-show-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!