简体   繁体   中英

I am not able to display graph in matplotlib

I'm trying to print a logistic differential equation and I'm pretty sure the equation is written correctly but my graph doesn't display anything.

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()

You get nothing in your plot because you are plotting points.

In plt you need to have x array and y array (that have the same length) in order to make a plot.

If you want to do exactly what you are doing I suggest to do like this:

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

I hope that this helps you ^^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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