在 Jupyter Notebook 中增加 Matplotlib .show() 的 DPI [英] Increase DPI of Matplotlib .show() in Jupyter Notebook
问题描述
我在 Jupyter Notebook 中使用 Matplotlib 来显示地图图像.代码如下所示:
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)
问题是,虽然plt.savefig('test.png', dpi = 300)
看起来不错(因为我把dpi改成300了),但是notebook中显示的图片就是这样低分辨率我无法做出任何事情,并且 plt.imshow(img, 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:
那么我想知道是否有办法更改 Jupyter Notebook 中显示的图像的分辨率?
So what I'm wondering is if there is a way to change the resolution of the image shown in the Jupyter Notebook?
推荐答案
在笔记本的开头添加:
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
就是这样!
这篇关于在 Jupyter Notebook 中增加 Matplotlib .show() 的 DPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!