问题描述
我希望 Matplotlib/Pyplot 生成具有一致画布大小的绘图.也就是说,图形可以有不同的大小以适应轴的描述,但绘图区域(绘制曲线的矩形)应始终具有相同的大小.
I would like Matplotlib/Pyplot to generate plots with a consistent canvas size. That is, the figures can well have different sizes to accomodate the axis descriptions, but the plotting area (the rectangle within which the curves are drawn) should always have the same size.
有没有一种简单的方法可以实现这一目标?pyplot.figure() 的选项 figsize 似乎设置了图形的整体大小,而不是画布的大小,因此每当轴描述占用更多或更少的空间时,我都会得到不同的画布大小.
Is there a simple way to achieve that? The option figsize of pyplot.figure() seems to set the overall size of the figure, not that of the canvas, so I get a different canvas size whenever the axis description occupies more or less space.
推荐答案
这是我对 Matplotlib 最大的挫折之一.我经常使用栅格数据,例如我想添加颜色图、图例和一些标题.matplotlib 库中的任何简单示例都将导致不同的分辨率并因此重新采样数据.尤其是在进行图像分析时,您不希望进行任何(不需要的)重采样.
This is one of my biggest frustrations with Matplotlib. I often work with raster data where for example i want to add a colormap, legend and some title. Any simple example from the matplotlib gallery doing so will result in a different resolution and therefore resampled data. Especially when doing image analysis you dont want any (unwanted) resampling.
这是我通常做的事情,尽管我很想知道是否有更简单或更好的方法.
Here is what i usually do, although i would love to know if there are simpler or better ways.
让我们开始加载图片并以相同的分辨率输出它:
Lets start with loading a picture and outputting it just as it is with the same resolution:
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('D:\mpl_logo.png', dpi=dpi, transparent=True)
请注意,我手动定义了轴位置,以便跨越整个图形.
Note that i manually defined the axes position so that spans the entire figure.
以与上述类似的方式,您可以在图像周围添加一些边距以允许标签或颜色条等.
In a similar way as above you could add some margin around the image to allow for labels or colorbars etc.
此示例在图像上方添加 20% 的边距,然后用于绘制标题:
This example adds a 20% margin above the image, which is then used for plotting a title:
fig = plt.figure(figsize=(xinch,yinch/.8))
ax = plt.axes([0., 0., 1., .8], frameon=False, xticks=[],yticks=[])
ax.imshow(img, interpolation='none')
ax.set_title('Matplotlib is fun!', size=16, weight='bold')
plt.savefig('D:\mpl_logo_with_title.png', dpi=dpi)
因此图形 y 大小(高度)增加,轴的 y 大小均等减小.这提供了更大的(整体)输出图像,但轴区域的大小仍然相同.
So the figure y-size (height) is increased and the y-size of the axes is decreased equally. This gives a larger (overall) output image, but the axes area will still be the same size.
拥有像 .set_scale() 这样的图形或轴属性来强制真正的 1-on-x 输出可能会很好.
It might be nice the have a figure or axes property like .set_scale() to force a true 1-on-x output.
这篇关于在 Matplotlib 中设置绘图画布的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!