改变matplotlib极地轮廓中的figsize

时间:2013-03-08 17:47:56

标签: matplotlib

我使用以下示例Example创建两个极坐标轮廓子图。当我创建pdf时,我想通过更改figsize删除大量的空格。

我知道如何通常更改figsize但是我很难看到在这个代码示例中将它放在哪里。任何指导或暗示都将不胜感激。

非常感谢!

import numpy as np
import matplotlib.pyplot as plt

#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 20))
zeniths = np.arange(0, 70, 10)

r, theta = np.meshgrid(zeniths, azimuths)
values = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
ax.contourf(theta, r, values)

plt.show()

2 个答案:

答案 0 :(得分:1)

您可以轻松地将plt.figsize(x,y)放在代码的开头,它会起作用。 plt.figsize改变所有未来图的大小,而不仅仅是当前的图。

但是,我认为你的问题不是你想象的那样。除非您更改选项,否则生成的PDF中往往会有相当多的空白。我通常使用

plt.savefig( 'name.pdf', bbox_inches='tight', pad_inches=0 )

这给尽可能少的空白。 bbox_inches ='tight'试图使边界框尽可能小,而pad_inches设置应该填充多少英寸的空白。在我的情况下,我根本没有额外的填充,因为我在我使用的数字中添加了填充。

答案 1 :(得分:1)

另一种方法是在figsize的通话中使用plt.subplots kwarg。

fig, ax = plt.subplots(figsize=(6,6), subplot_kw=dict(projection='polar'))

顺便说一句,这些值以英寸为单位。