简体   繁体   English

为什么我在使用 matplotlib 时得到一个空的 plot?

[英]Why do I get an empty plot while using matplotlib?

I was trying to create a bifurcation diagram using pyplot in Python 3. I first defined a function for the logistic map and then used it in a second function in a for loop to generate the bifurcation diagram. I was trying to create a bifurcation diagram using pyplot in Python 3. I first defined a function for the logistic map and then used it in a second function in a for loop to generate the bifurcation diagram. The following is my code:以下是我的代码:

import numpy as np
import matplotlib.pyplot as plt

def logistic(n, r, x0):
    xn = []
    for i in range(1,n+1):
        x0 = r*x0*(1-x0)
        xn += [x0]
        xn = np.array(xn)
        return xn
        
def bifurcation(n, k, rmin, rmax, rstep, x0):
    for r in np.arange(rmin, rmax, rstep):
        traj = logistic(n, r, x0)
        R = r*np.ones([1, n+1])
        plt.scatter(R[k:n], traj[k:n])
    plt.xlim([rmin, rmax])
    plt.ylim([0, 1])
    plt.show()

bifurcation(50, 5, 2.4, 4, 0.1, 0.2)

The problem is that I get a blank plot as the output.问题是我得到一个空白的 plot 作为 output。 No points are plotted at all.根本没有绘制任何点。 What am I doing wrong here?我在这里做错了什么?

import numpy as np
import matplotlib.pyplot as plt

def logistic(n, r, x0):
    xn = []
    for i in range(1,n+1):
        x0 = r*x0*(1-x0)
        xn += [x0]

    xn = np.array(xn)
    #print(xn.shape)
    return xn
        
def bifurcation(n, k, rmin, rmax, rstep, x0):
    for r in np.arange(rmin, rmax, rstep):
        traj = logistic(n, r, x0)
        R = r*np.ones(shape=n)
        plt.scatter(R[k:n], traj[k:n])
        #print(R.shape)

    plt.xlim([rmin, rmax])
    plt.ylim([0, 1])
    plt.show()

bifurcation(50, 5, 2.4, 4, 0.1, 0.2)

you forgot tabs in logistic() and np.ones vas a bit strange你忘记了logistic()和np.ones vas中的标签有点奇怪

When I first ran your code, I noticed that R[k:n] and traj[k:n] were both empty.当我第一次运行您的代码时,我注意到R[k:n]traj[k:n]都是空的。 It turns out that you've defined R such that there is a list inside of a list, and it's the inner list that has the numbers.事实证明,您已经定义R以便在列表内部有一个列表,并且它是具有数字的内部列表。

Thus, I just grabbed the inner list rather than using the outer one.因此,我只是抓住了内部列表而不是使用外部列表。 So R = r*np.ones([1, n+1]) became R = r*np.ones([1, n+1])[0] .所以R = r*np.ones([1, n+1])变成R = r*np.ones([1, n+1])[0] (There may be a more elegant way of doing this, but this should suffice.) (可能有一种更优雅的方式来做到这一点,但这应该就足够了。)

Then, for traj , it looks like traj only computes a single number (and puts it inside of a list), but in order to graph information, you need to have each of the x and y lists for the scatter function to have the right size.然后,对于traj ,看起来 traj 只计算一个数字(并将其放在列表中),但为了绘制信息,您需要拥有scatter function 的每个xy列表才能拥有正确的尺寸。 So I just duplicated the single number as many times as needed ( nk times).所以我只是根据需要多次复制单个数字( nk次)。 So traj[k:n] should instead be [traj[0]] * (nk) .所以traj[k:n]应该改为[traj[0]] * (nk)

And then we get a graph:然后我们得到一个图表: 代码工作图

Here's the full code:这是完整的代码:

import numpy as np
import matplotlib.pyplot as plt

def logistic(n, r, x0):
    xn = []
    for i in range(1,n+1):
        x0 = r*x0*(1-x0)
        xn += [x0]
        xn = np.array(xn)
        return xn

def bifurcation(n, k, rmin, rmax, rstep, x0):
    for r in np.arange(rmin, rmax, rstep):
        traj = logistic(n, r, x0)
        R = r*np.ones([1, n+1])[0]
        R_list = R[k:n]
        traj_list = [traj[0]] * (n-k)
        plt.scatter(R_list, traj_list)
    plt.xlim([rmin, rmax])
    plt.ylim([0, 1])
    plt.show()

bifurcation(50, 5, 2.4, 4, 0.1, 0.2)

Not sure if this is what you were looking for, but it seems to solve your problem of not getting a graph to show up.不确定这是否是您想要的,但它似乎解决了您没有显示图表的问题。 Hopefully it's helpful.希望它会有所帮助。

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

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