我认为你可以使用与matlab相同的代码 struct 得到相同的行为.下面的代码将生成两个图,每个图有4个图,而不使用子图.
在matplotlib中,子图用于在同一图形上显示多个图形,但这里您希望在同一图形上添加多个图形.
你可以回到一个数字添加图例,标题,定义轴限制,显示任何格式,如网格,股票等.
import numpy as np
import matplotlib.pyplot as plt
# Create the x vector
x = np.arange(0, 20.2, 0.2)
# Loop over the values of n
for n in [1, 2, 3, 4]:
# Plot n*x for each n in the first figure
plt.figure(1)
plt.plot(x, n*x, label=f'n={n}')
# Plot x^n for each n in the second figure
plt.figure(2)
plt.plot(x, x**n, label=f'n={n}')
# Format the first figure
# title, legends, grids, tickers, axis limits, colors, ...
plt.figure(1)
plt.legend()
plt.title('n*x for n in [1, 2, 3, 4]')
plt.xlabel('x')
plt.ylabel('n*x')
# Format the second figure.
plt.figure(2)
plt.legend()
plt.title('x^n for n in [1, 2, 3, 4]')
plt.xlabel('x')
plt.ylabel('x^n')
# Show all figures
plt.show()