如何使用matplotlib定义图中子图的尺寸(以英寸为单位)?

时间:2015-05-28 07:40:39

标签: python matplotlib

我的数字的尺寸为figsize =(10,10),其中有一个子图。如何将子图的尺寸定义为8英寸宽x 8英寸H,它显示的子图的数据范围是什么?

编辑:我正在寻找这个的原因是我正在尝试创建一些用于发布的图表。所以我需要我的情节有一个固定的宽度和高度,以便能够插入manuscrpipt seamsly。

以下是条纹代码:

map_size = [8,8]
fig, ax = plt.subplots(1,1,figsize=map_size)
ax.plot(data)
fig.savefig('img.png', dpi=300,)  # figure is the desired size, subplot seems to be scaled to fit to fig

1 个答案:

答案 0 :(得分:3)

一种选择是使用fig.subplots_adjust来设置图中子图的大小。参数leftrightbottomtop是小数单位(总图数维度)。因此,对于10x10数字中的8x8子图,right - left = 0.8等:

import matplotlib.pyplot as plt

fig=plt.figure(figsize=(10.,10.))
fig.subplots_adjust(left=0.1,right=0.9,bottom=0.1,top=0.9)

ax=fig.add_subplot(111)

这样做,如果更改图形尺寸,则必须手动更改左,右,底部和顶部值。

我猜你可以在你的代码中构建这个计算:

import matplotlib.pyplot as plt

subplotsize=[8.,8.]
figuresize=[10.,10.]   

left = 0.5*(1.-subplotsize[0]/figuresize[0])
right = 1.-left
bottom = 0.5*(1.-subplotsize[1]/figuresize[1])
top = 1.-bottom

fig=plt.figure(figsize=(figuresize[0],figuresize[1]))
fig.subplots_adjust(left=left,right=right,bottom=bottom,top=top)

ax=fig.add_subplot(111)
相关问题