Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

python - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Add this at the beginning of the notebook:

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

That's it !


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...