首页 > 解决方案 > 如何使用轴和子图设置 matplotlib figsize

问题描述

您好,我有一个非常基本的 matplotlib 绘图,但我希望能够设置图形大小,我对不使用多个子图时如何执行此操作感到困惑。我对 figsize 和 subplots 之间的关系感到困惑,请参见下面的代码:

plt.figure(figsize=(10,10))    

y = np.arange(1,6,1)
y1 = y**2
y2 = y**3
y3 = y**4
x = np.arange(1,101,20)

fig, ax = plt.subplots()
ax.plot(x,y1)
ax.plot(x,y2)
ax.plot(x,y3)

标签: pythonmatplotlibjupyter-notebook

解决方案


只需放入 1 行和 1 列子图,就可以得到一个图。

fig, ax = plt.subplots(1, 1, figsize=(8,8))

y = np.arange(1,6,1)
y1 = y**2
y2 = y**3
y3 = y**4
x = np.arange(1,101,20)

fig, ax = plt.subplots()
ax.plot(x,y1)
ax.plot(x,y2)
ax.plot(x,y3)

fig.savefig('eightbyeight.png', dpi = 300)

推荐阅读