简体   繁体   中英

plotting a sequence results in: “ValueError: x and y must have same first dimension”

I'm a beginner here and I'm experiencing a problem when I'm trying to plot a sequence:

My code gives me this error message: "ValueError: x and y must have same first dimension"

hold(True)
x=[0.5]
for r in arange(2,4,0.1):
    for i in range(170):
        xn= x[-1]
        xnp1 = r*xn*(1-xn)
        x.append(xnp1)
        xa=array(x)   
        rplot=0*xa+r
plot (rplot, xn,".")
show()

You're trying to plot xn (the dependent variable, aka "the y-data") versus rplot (the independent variable, aka "the x-data").

From your code, you'll see that xn refers to the last item in the array x , so it is a single scalar. rplot however is as long as the array x , because you made it such:

rplot = 0*xa + r

It's like plotting the number "1" to more than one x-value (like [1, 2, 3] ). You can see that this is confusing, right?

It's not clear what exactly you want to plot (which variables against which other variables), but this post does answer why you're getting that error. Perhaps it'll help you in finding out what you really want to plot.

Based on the received comment, you might be looking for this code:

hold(True)
x = [0.5]
for r in arange(2,4,0.1):
    for _ in range(170):
        xn = x[-1]
        x.append( r*xn*(1 - xn) )
    rplot = np.full(len(x), r)            
    plot(rplot, x, ".")
show()

It plots, for every r within the range(2, 4, .1) an array of points, which depend on that value. The array of points for that particular value of r all have the same ordinate, which is r . It looks like stripes.

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