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."
import matplotlib.pyplot as plt
import random
list1 = range(5)
plt.plot(list1)
[<matplotlib.lines.Line2D at 0x12330de20>]
list2 = [random.random() for _ in range(5)]
print(list2)
[0.6023701644412124, 0.9214737578152091, 0.3815190793139095, 0.07394087879905908, 0.03513324571452825]
plt.plot(list1, list2)
[<matplotlib.lines.Line2D at 0x122e7eb40>]
plt.plot(list1, list2, 'gs') # try: 'r^' 'gs' 'k--' 'b:' 'ro-'
[<matplotlib.lines.Line2D at 0x123354dd0>]
plt.scatter(list1, list2)
<matplotlib.collections.PathCollection at 0x123257c80>
plt.bar(list1, list2, .7)
<BarContainer object of 5 artists>
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)]
plt.plot(t, t2)
[<matplotlib.lines.Line2D at 0x1235b52e0>]
plt.plot(t, t2, 'r', t, t3, 'g')
[<matplotlib.lines.Line2D at 0x1235dcd10>, <matplotlib.lines.Line2D at 0x12371a330>]
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()
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()
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.
import math
math.factorial(5)
120
Write a comb
function to calculate the binomial coefficient (from Python 3.8 there also exists a function math.comb()
for this):
def comb(n, m):
return math.factorial(n)//math.factorial(m)//math.factorial(n-m)
comb(25, 10)
3268760
math.exp(1), math.exp(2) # the e^x function
(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.
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)
binomial_dist, poison_dist
(0.2907100492017224, 0.2623272261632803)