[Tutor] ploting of a square well

Peter Otten __peter__ at web.de
Sun Feb 5 18:42:38 CET 2012


Debashish Saha wrote:

> from pylab import *
> x=linspace(1,1000,10000)
> a,b = 10,20
> def v(x):
>     i=5;v0=2
>     for n in range(1,1+i):
>         if x>(n-1)*(a+b) and x<a+(n-1)*(a+b):
>             return 0
>         elif x>a+(n-1)*(a+b) and x<(n)*(a+b) :
>             return v0
> #v=vectorize(v)
> plot(x,v)
> show()
> 
> Error:
> x and y must have same first dimension
> Question:
> what is to be edited?

When you want to plot a graph you can't pass the function as an argument.

import pylab
def f(x): return x*x
x = [1, 2, 3]
#wrong, one list and a function
pylab.plot(x, f) # error
pylab.show()

You must instead pass the values, one list with the x-values and another 
with the same number of y-values:

import pylab
def f(x): return x*x
x = [1, 2, 3]
y = [f(xi) for xi in x]
#correct, two lists
pylab.plot(x, y)
pylab.show()




More information about the Tutor mailing list