繁体   English   中英

我无法在 matplotlib 中显示图形

[英]I am not able to display graph in matplotlib

我正在尝试打印一个逻辑微分方程,我很确定方程写得正确,但我的图表没有显示任何内容。

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

def eq(con,x):
    return con*x*(1-x)

xList = np.linspace(0,4, num=1000)
con = 2.6
x= .4

for num in range(len(xList)-1):
    plt.plot(xList[num], eq(con,x))
    x=eq(con,x)

plt.xlabel('Time')
plt.ylabel('Population')
plt.title("Logistic Differential Equation")

plt.show()

你在你的情节中一无所获,因为你正在绘制点。

在 plt 中,您需要有 x 数组和 y 数组(具有相同的长度)才能绘制图。

如果你想做你正在做的事情,我建议这样做:

import matplotlyb.pyplot as plt # just plt is sufficent 
import numpy as np
def eq(con,x):
    return con*x*(1-x)

xList = np.linspace(0,4, num=1000)
con = 2.6
x= .4

y = np.zeros(len(xList)) # initialize an array with the same lenght as xList
for num in range(len(xList)-1):
    y[num] = eq(con,x)
    x=eq(con,x)

plt.figure() # A good habit is always to use figures in plt 
plt.plot(xList, y) # 2 arrays of the same lenght
plt.xlabel('Time')
plt.ylabel('Population')
plt.title("Logistic Differential Equation")

plt.show() # now you should get somthing here

我希望这对你有帮助^^

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM