在Matplotlib中设置绘图画布的大小 [英] Setting the size of the plotting canvas in Matplotlib

查看:208
本文介绍了在Matplotlib中设置绘图画布的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望Matplotlib / Pyplot生成一致的画布大小的图。也就是说,这些数字可以具有不同的大小以容纳轴描述,但绘图区域(绘制曲线的矩形)应始终具有相同的大小。



<有没有一种简单的方法来实现呢? pyplot.figure()的选项figsize似乎设置了图形的整体大小,而不是画布的大小,所以当轴描述占用更多或更少的空间时,我会得到不同的画布大小。

解决方案

这是我对Matplotlib最大的挫折之一。我经常使用栅格数据,例如我想添加一个颜色表,图例和一些标题。 matplotlib库中的任何简单示例都会导致不同的分辨率,因此会重新采样数据。特别是在进行图像分析时,您不需要任何(不需要的)重采样。



这就是我通常所做的,尽管我很想知道是否有更简单或更好的方法。



让我们开始加载一张图片并输出,就像它具有相同的分辨率一样:

  import matplotlib.pyplot as plt 
import urllib2

#载入图片
img = plt.imread(urllib2.urlopen('http:// upload.wikimedia.org/wikipedia/en/thumb/5/56/Matplotlib_logo.svg/500px-Matplotlib_logo.svg.png'))

#获取尺寸
ypixels,xpixels, bands = img.shape

#以英寸为单位获得大小
dpi = 72.
xinch = xpixels / dpi
yinch = ypixels / dpi

#绘制并保存为与原始
相同的尺寸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)

请注意,我手动定义轴位置,以便跨越整个数字。



以上述类似的方式,您可以在图像周围添加一些边距以允许使用标签或颜色条等。 b
$ b

这个例子在图片上方增加了20%的边距,然后用于绘制标题:

  fig = plt.figure(figsize =(xinch,yinch / .8))

ax = plt.axes([0,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值也会相应减小。这会产生更大(整体)的输出图像,但轴区域的大小仍然相同。

使用.set_scale()等图形或坐标轴属性强制产生真正的1-on-x输出可能会更好。

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.

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.

解决方案

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.

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)

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.

It might be nice the have a figure or axes property like .set_scale() to force a true 1-on-x output.

这篇关于在Matplotlib中设置绘图画布的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆