Python 在 Matplotlib 中设置绘图画布的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16057869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the size of the plotting canvas in Matplotlib
提问by Dario
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.
我希望 Matplotlib/Pyplot 生成具有一致画布大小的图。也就是说,图形可以具有不同的大小以适应轴描述,但绘图区域(绘制曲线的矩形)应始终具有相同的大小。
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.
有没有简单的方法来实现这一目标?pyplot.figure() 的选项 figsize 似乎设置了图形的整体大小,而不是画布的大小,因此每当轴描述占用更多或更少空间时,我都会得到不同的画布大小。
采纳答案by Rutger Kassies
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.
这是我对 Matplotlib 的最大挫折之一。我经常使用栅格数据,例如我想添加颜色图、图例和一些标题。matplotlib 库中的任何简单示例都将导致不同的分辨率并因此重新采样数据。特别是在进行图像分析时,您不希望进行任何(不需要的)重采样。
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.
以与上述类似的方式,您可以在图像周围添加一些边距以允许标签或颜色条等。
This example adds a 20% margin above the image, which is then used for plotting a title:
此示例在图像上方添加 20% 的边距,然后用于绘制标题:
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)
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.
因此图形 y 大小(高度)增加,轴的 y 大小同样减小。这提供了更大的(整体)输出图像,但轴区域的大小仍然相同。
It might be nice the have a figure or axes property like .set_scale() to force a true 1-on-x output.
拥有像 .set_scale() 这样的图形或轴属性来强制真正的 1-on-x 输出可能会很好。
回答by Piotr Jurkiewicz
You can specify the following settings in Matplotlib rc:
您可以在 Matplotlib rc 中指定以下设置:
import matplotlib
matplotlib.rcParams['figure.figsize'] = [10, 10] # for square canvas
matplotlib.rcParams['figure.subplot.left'] = 0
matplotlib.rcParams['figure.subplot.bottom'] = 0
matplotlib.rcParams['figure.subplot.right'] = 1
matplotlib.rcParams['figure.subplot.top'] = 1

