在Jupyter Notebook中增加Matplotlib .show()的DPI [英] Increase DPI of Matplotlib .show() in Jupyter Notebook
问题描述
我正在Jupyter笔记本中使用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),但笔记本中显示的图像分辨率太低,无法对其进行任何显示,而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笔记本中显示的图像的分辨率?
So what I'm wondering is if there is a way to change the resolution of the image shown in the Jupyter Notebook?
非常感谢Alex
答案:
Politinsa建议我将其添加到文件的开头:
Politinsa suggested I add this to the beginning of the file:
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
它可以解决问题.
推荐答案
将此内容添加到笔记本的开头:
Add this at the beginning of the notebook:
import matplotlib as mpl
mpl.rcParams['figure.dpi'] = 300
就是这样!
这篇关于在Jupyter Notebook中增加Matplotlib .show()的DPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!