Plotting in Python¶

"Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python." There are several tutorials, where you can learn the usage. "matplotlib.pyplot is a collection of functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc."

In [1]:
import matplotlib.pyplot as plt
import random
In [2]:
list1 = range(5)
In [3]:
plt.plot(list1)
Out[3]:
[<matplotlib.lines.Line2D at 0x12330de20>]
No description has been provided for this image
In [4]:
list2 = [random.random() for _ in range(5)]
print(list2)
[0.6023701644412124, 0.9214737578152091, 0.3815190793139095, 0.07394087879905908, 0.03513324571452825]
In [5]:
plt.plot(list1, list2)
Out[5]:
[<matplotlib.lines.Line2D at 0x122e7eb40>]
No description has been provided for this image
In [6]:
plt.plot(list1, list2, 'gs') # try: 'r^' 'gs' 'k--' 'b:' 'ro-'
Out[6]:
[<matplotlib.lines.Line2D at 0x123354dd0>]
No description has been provided for this image
In [7]:
plt.scatter(list1, list2)
Out[7]:
<matplotlib.collections.PathCollection at 0x123257c80>
No description has been provided for this image
In [8]:
plt.bar(list1, list2, .7)
Out[8]:
<BarContainer object of 5 artists>
No description has been provided for this image
In [9]:
t = [i*0.01 for i in range(251)]
t2 = [(i*0.01)**2 for i in range(251)]
t3 = [(i*0.01)**3 for i in range(251)]
In [10]:
plt.plot(t, t2)
Out[10]:
[<matplotlib.lines.Line2D at 0x1235b52e0>]
No description has been provided for this image
In [11]:
plt.plot(t, t2, 'r', t, t3, 'g')
Out[11]:
[<matplotlib.lines.Line2D at 0x1235dcd10>,
 <matplotlib.lines.Line2D at 0x12371a330>]
No description has been provided for this image
In [12]:
plt.figure(figsize=(12, 3))

plt.subplot(131)
plt.bar(list1,list2)

plt.subplot(132)
plt.scatter(list1,list2)

plt.subplot(133)
plt.plot(list1,list2)

plt.suptitle('Functions')
plt.show()
No description has been provided for this image
In [13]:
plt.figure(figsize=(8, 12))

plt.subplot(311)
plt.bar(list1,list2)
plt.title("Bar")

plt.subplot(312)
plt.scatter(list1,list2)
plt.title("Scattered")

plt.subplot(313)
plt.plot(list1,list2)
plt.title("Plot")

plt.suptitle('Functions')

plt.tight_layout()         
plt.show()
No description has been provided for this image

Exercise: Copy the next code into a file named 4test.py and run it from a terminal with the command python3 4test.py 4

import matplotlib.pyplot as plt
import random
import sys

def my_plot():
    """
    Plotting n random values.
    n is read in from the terminal.
    """
    
    n = int(sys.argv[1])
    
    list1 = range(n)
    list2 = [random.random() for _ in range(n)]
    list3 = [i**2 for i in list2]
    
    plt.subplot(311)
    plt.bar(list1,list1)
    plt.subplot(312)
    plt.bar(list1,list2)
    plt.subplot(313)
    plt.bar(list1,list3)
    plt.show()

def main():
    my_plot()

if __name__ == "__main__":
    main()

For calculating factorial, binomial coefficient, exponential function you may use the math module:

Using the math module¶

To compute the factorial or the exponential function you can use the math module.

In [14]:
import math

math.factorial(5)
Out[14]:
120

Write a comb function to calculate the binomial coefficient (from Python 3.8 there also exists a function math.comb() for this):

In [15]:
def comb(n, m):
    return math.factorial(n)//math.factorial(m)//math.factorial(n-m)
In [16]:
comb(25, 10)
Out[16]:
3268760
In [17]:
math.exp(1), math.exp(2) # the e^x function
Out[17]:
(2.718281828459045, 7.38905609893065)

Exercise: Throw a die 10 times. What is the probability that you get 6 exactly 2 times. Calculate with the formula for the binomial distribution (the parameters will be $n=10$, $p=1/6$), and approximate it with Poisson-distribution, with the parameter $\lambda$, where $\lambda=n\cdot p$ is the expected value of the binomial distribution.

In [18]:
n = 10
p = 1/6
m = 2
l = n*p  # lambda

binomial_dist = comb(n, m) * p**m * (1-p)**(n-m)
poison_dist = l**m * math.exp(-l) / math.factorial(m)
In [19]:
binomial_dist, poison_dist
Out[19]:
(0.2907100492017224, 0.2623272261632803)
In [ ]: