在 matplotlib 中设置子图的大小
Set size of subplot in matplotlib
我想知道当图形包含多个子图(在我的例子中是 5 × 2)时如何设置子图的大小。无论我允许整个图形有多大,子图似乎总是很小。我想直接控制图中子图的大小。代码的简化版本粘贴在下面。
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(20)
y = np.random.randn(20)
fig = plt.figure(figsize=(20, 8))
for i in range(0,10):
ax = fig.add_subplot(5, 2, i+1)
plt.plot(x, y, 'o')
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
# x and y axis should be equal length
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
ax.set_aspect(abs(x1-x0)/abs(y1-y0))
plt.show()
fig.savefig('plot.pdf', bbox_inches='tight')
只需切换图形大小宽度和高度即可:
fig = plt.figure(figsize=(20, 8))
至:
fig = plt.figure(figsize=(8, 20))
为您的情节使用整个页面。
这将使您的剧情从:
至:
我想知道当图形包含多个子图(在我的例子中是 5 × 2)时如何设置子图的大小。无论我允许整个图形有多大,子图似乎总是很小。我想直接控制图中子图的大小。代码的简化版本粘贴在下面。
import numpy as np
import matplotlib.pyplot as plt
x = np.random.randn(20)
y = np.random.randn(20)
fig = plt.figure(figsize=(20, 8))
for i in range(0,10):
ax = fig.add_subplot(5, 2, i+1)
plt.plot(x, y, 'o')
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
# x and y axis should be equal length
x0,x1 = ax.get_xlim()
y0,y1 = ax.get_ylim()
ax.set_aspect(abs(x1-x0)/abs(y1-y0))
plt.show()
fig.savefig('plot.pdf', bbox_inches='tight')
只需切换图形大小宽度和高度即可:
fig = plt.figure(figsize=(20, 8))
至:
fig = plt.figure(figsize=(8, 20))
为您的情节使用整个页面。
这将使您的剧情从:
至: