Solved

Python plotting

Posted on 2013-01-10
2
327 Views
Last Modified: 2013-01-10
Hi

Can anybody tell me why the following code won't work? I get a syntax error on

plt.figure()

This is the code:

import matplotlib.pyplot as plt
import math

def plotBinomial():
      prob = input( 'Enter probability of success: ')
      n = input( 'Enter the number of successes: ')
      Nmax = input( 'Enter the maximum number of trials: ')

      x = []
      y = []
      
      for N in range( n, Nmax + 1 ):
            x.append( N )
            y.append( probability( prob, n, N )
            
      plt.figure()
      plt.title( 'Varying N and probability p = ' + str( prob ) )
      plt.xlabel( 'N' )
      plt.ylabel( 'Probability' )
      plt.grid( True )
      plt.plot( x, y )
      plt.show()
      plt.close()

def probability( prob, n, N ):
    nCr = math.factorial( N ) / ( math.factorial( n ) * math.factorial( N - n ) )
    P = nCr * ( prob**n ) * ( 1 - prob )**( N - n )
    return P
0
Comment
Question by:IssacJones
2 Comments
 
LVL 16

Accepted Solution

by:
gelonida earned 300 total points
ID: 38762646
You forgot a closing parenthesis in the line before.


            y.append( probability( prob, n, N )

should be
            y.append( probability( prob, n, N ))

This is a rather classical issue with syntax checking.
The parser doesn't know whether you forgot the closing parenthesis or whether you wanted to write a multi-line statement.
0
 

Author Closing Comment

by:IssacJones
ID: 38762795
Excellent!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now