这是我对Matplotlib最大的挫折之一。我经常使用光栅数据,例如我想在其中添加彩色地图、图例和一些标题。matplotlib库中的任何简单示例都会导致分辨率不同,因此会重新采样数据。尤其是在做图像分析的时候,你不需要任何(不需要的)重采样。
这是我通常做的,尽管我想知道是否有更简单或更好的方法。
让我们从加载图片并以相同分辨率输出图片开始:import matplotlib.pyplot as plt
import urllib2
# load the image
img = plt.imread(urllib2.urlopen('http://upload.wikimedia.org/wikipedia/en/thumb/5/56/Matplotlib_logo.svg/500px-Matplotlib_logo.svg.png'))
# get the dimensions
ypixels, xpixels, bands = img.shape
# get the size in inches
dpi = 72.
xinch = xpixels / dpi
yinch = ypixels / dpi
# plot and save in the same size as the original
fig = plt.figure(figsize=(xinch,yinch))
ax = plt.axes([0., 0., 1., 1.], frameon=False, xticks=[],yticks=[])
ax.imshow(img, interpolation='none')
plt.savefig('