把三个子表之间的距离调大修改下面的代码import matplotlib.pyplot as plt import numpy as np fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(6, 4)) fig.canvas.manager.set_window_title("袁全21003170121") x1 = np.arange(0, 6, 0.01) y1 = np.square(x1) ax1.plot(x1, y1, "r-", linewidth=2.0) ax1.set_title("y=x的平方数", fontproperties="SimHei") ax1.grid(True) ax1.patch.set_facecolor("LightGray") x2 = np.arange(0, 6, 0.01) y2 = np.square(np.sin(3 * x2)) ax2.plot(x2, y2, "g-", linewidth=2.0) ax2.set_title("y=sin(3x)的平方数", fontproperties="SimHei") ax2.grid(True) ax2.patch.set_facecolor("Cornsilk") x3 = np.arange(0, 6, 0.01) y3 = np.cos(2 * x3) ax3.plot(x3, y3, "b-", linewidth=2.0) ax3.set_title("y=cos(2x)", fontproperties="SimHei") ax3.grid(True) ax3.patch.set_facecolor("Azure") plt.show()
时间: 2023-10-30 20:07:09 浏览: 98
你可以通过修改每个子图之间的间距来实现这个要求。可以使用`fig.subplots_adjust()`函数来调整子图的间距。例如,将每个子图之间的间距设置为0.5,可以将代码修改如下:
```
import matplotlib.pyplot as plt
import numpy as np
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(6, 4))
fig.subplots_adjust(hspace=0.5) # 调整子图之间的间距
fig.canvas.manager.set_window_title("袁全21003170121")
x1 = np.arange(0, 6, 0.01)
y1 = np.square(x1)
ax1.plot(x1, y1, "r-", linewidth=2.0)
ax1.set_title("y=x的平方数", fontproperties="SimHei")
ax1.grid(True)
ax1.patch.set_facecolor("LightGray")
x2 = np.arange(0, 6, 0.01)
y2 = np.square(np.sin(3 * x2))
ax2.plot(x2, y2, "g-", linewidth=2.0)
ax2.set_title("y=sin(3x)的平方数", fontproperties="SimHei")
ax2.grid(True)
ax2.patch.set_facecolor("Cornsilk")
x3 = np.arange(0, 6, 0.01)
y3 = np.cos(2 * x3)
ax3.plot(x3, y3, "b-", linewidth=2.0)
ax3.set_title("y=cos(2x)", fontproperties="SimHei")
ax3.grid(True)
ax3.patch.set_facecolor("Azure")
plt.show()
```
你可以根据需要适当调整子图之间的间距。
阅读全文