0

I'm trying to plot a line graph in Python using pyplot but I get an error, TypeError: 'float' object is not callable for this line of code:

ycoord = [math.pow(p(1-p),(i-1)) for i in range(1,51)]

I basically have a function p(1-p)^(i-1) that I need to plot a line graph with. I have an array of coordinates 1-51. And I need to get the corresponding y coordinates with that function. I have also tried: p(1-p)**(i-1), but that has not worked either.

Here is my full code:

def test():
    p = 0.2
    plt.figure(1)
    #create x coordinates [1-51]
    xcoord = [i for i in range(1,51)]
    #create y coordinates from formula
    ycoord = [math.pow(p(1-p),(i-1)) for i in range(1,51)]
    plt.plot(xcoord,ycoord)
    plt.draw()
    plt.show()

test()
2
  • You have p(1-p) where p is a float, what do you want to do? Commented Feb 11, 2017 at 2:27
  • In Python (and most other computer languages I know of), p(1-p) does not mean p times (1-p). You have to explicitly write the multiplication operator. i.e.: p * (1-p). Unlike many other languages, the exponentiation operator in Python is written ** not ^. Commented Feb 11, 2017 at 2:43

2 Answers 2

1

The syntax for multiplication in Python is not the same as you may be used to when writing math equations, you can't omit the '*' symbol.

p (1-p)

should be:

p * (1-p) 
Sign up to request clarification or add additional context in comments.

Comments

1

The error is self explanatory,

 ycoord = [math.pow(p(1-p),(i-1)) for i in range(1,51)]

In this block p is a floating point number, not a method name. If you have a function p that you want to call from here then rename this variable p to something else or vice versa.

Also if you meant (px(1-p))^(i-1) [p times (1-p) to the power i-1], you should do pow(p*(1-p), (i-1))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.