如何在同一张图像上获得所有绘图?

问题描述 投票:0回答:1

我想绘制结果在不同的初始条件后如何变化,但是当所有结果都在不同的图像上时,我没有得到太多信息,所以我想以某种方式将它们全部放在同一张图像上。问题是我不知道如何做到这一点,因为我不明白如何使其工作,因为值在每组初始条件后都会发生变化。我输入的代码如下:

import numpy as np
from numpy.linalg import eig
import matplotlib.pyplot as plt

t_start, t_end, num_points = 0, 10, 100
dt = (t_end - t_start) / (num_points - 1)

x_system_values = np.zeros(num_points)
y_system_values = np.zeros(num_points)

a = float(input("Enter the coefficient ’a’: "))
b = float(input("Enter the coefficient ’b’: "))
c = float(input("Enter the coefficient ’c’: "))
d = float(input("Enter the coefficient ’d’: "))

    
for m in range(0,2):
    for n in range(0,2):
        x_system, y_system = m, n 
        for i in range(num_points):
            dxdt_system = a * x_system + b * y_system
            dydt_system = c * x_system + d * y_system
            x_system += dxdt_system * dt
            y_system += dydt_system * dt
            x_system_values[i] = x_system
            y_system_values[i] = y_system
        plt.figure()
        plt.plot(x_system_values, y_system_values)
        plt.xlabel("X")
        plt.ylabel("Y")
        plt.title("Stability")
        plt.legend()

plt.show()

我尝试更改命令的顺序,就像我将

plt.figure()
放在
for
之上,这样也许它会包含所有命令。我想将数组值放入向量中来存储它们,然后为矩阵中的每一列或每一行绘制一个图,但我认为我可能过于复杂化了。

python matplotlib
1个回答
0
投票

尝试使用子图。这将允许您将所有内容组织到同一个图形上。

import numpy as np
from numpy.linalg import eig
import matplotlib.pyplot as plt

t_start, t_end, num_points = 0, 10, 100
dt = (t_end - t_start) / (num_points - 1)

x_system_values = np.zeros(num_points)
y_system_values = np.zeros(num_points)

a = float(input("Enter the coefficient ’a’: "))
b = float(input("Enter the coefficient ’b’: "))
c = float(input("Enter the coefficient ’c’: "))
d = float(input("Enter the coefficient ’d’: "))

fig = plt.figure(figsize=(10, 10))
 
for m in range(0,2):
    for n in range(0,2):
        x_system, y_system = m, n 
        for i in range(num_points):
            dxdt_system = a * x_system + b * y_system
            dydt_system = c * x_system + d * y_system
            x_system += dxdt_system * dt
            y_system += dydt_system * dt
            x_system_values[i] = x_system
            y_system_values[i] = y_system
        ax = fig.add_subplot(2, 2, 2*m+n+1)
        ax.plot(x_system_values, y_system_values)
        ax.set_xlabel("X")
        ax.set_ylabel("Y")
        ax.set_title("Stability")

plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.