Python (3) Leaflet: Roland Becker December 16, 2020
Python (3) Leaflet: Roland Becker December 16, 2020
Python (3) Leaflet: Roland Becker December 16, 2020
Roland Becker
December 16, 2020
Contents
1 Basic language 1
2 Classes 4
3 Packages 5
4 Files 5
5 Numpy (basics) 6
6 Matplotib 8
7 Sympy 13
8 Scipy 13
9 Pandas 14
10 tkinter 15
1 Basic language
Reserved keywords (complete)
and, as, assert, break, class, continue, def, del, elif, else, except, False,
finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or,
pass, raise, return, True, try, while, with, yield
Errors/Exceptions (incomplete)
1
Basic Datatypes
Compound Datatypes
2
Built-in functions
h) sorted(a)
i) hasattr, getattr, setattr
j) map, set, zip, enumerate
Conditionals
if condition:
do someting
elif condition2:
do someting2
else:
do someting3
Loops
or
3
Functions
a) general definition
b) keyword arguments
2 Classes
Basics
class ParentClass():
def __init__(self, arg):
self.arg = arg
def __repr__(self): return "ParentClass" #used in ’print’
def compute(self): return self.arg**2
class Class(ParentClass):
def __init__(self, arg, arg2):
ParentClass.__init__(self, arg) # don’t forget ’self’
# or super().__init__(arg) no ’self’, allows for mulit-inheritance
self.arg2 = arg2
def __call__(self): return self.compute()+self.arg2
cls = Class(arg1, arg2)
vars(cls) # gets all elements of instance \python{cls}
cls() # calls __call__
# set an attribute by variable in a class
setattr(self, name, value)
# check if a class has an attribute by variable name
hasattr(self, name)
4
Advanced
import atexit
def __init___(self):
# usual stuff
atexit.register(self.function_at_delete)
3 Packages
Import
import something
# ’something’ can be the name of a local file ’something.py’
# or a package...
from something import somepart
# ’somepart’ can be a function, int or else in file ’something.py’
import numpy as np # giving a short name
import matplotlib.pyplot as plt # import a sub-module
Possible problems
When you import a package, it can do different things (specified in the file init .py in that package), for example
in can include subpackages or not
import scypy
scipy.integrate.solve_ivp() # will not work, need to import scipy.integrate
4 Files
For reading writing special file formats there are built-in libraries as wav, aifc, zipfile, PyPDF2, xlwings (excel),
Pillow (images).
Read
5
Write
import glob
filenames = glob.glob(’*.gif’) # show all gif-files
# alternative
import pathlib
p = Path(’.’)
filenames = p.glob(’*.gif’)
# delete a file
import os
if os.path.exists(filename): os.remove(filename)
else: print("The file does not exist")
# delete a folder
os.rmdir(foldername)
5 Numpy (basics)
(nd)array
ndarray is a n-dimensional array (rectangle in n dimensions) with homogenous data. The dimensions are called
axes. Best reference: numpy.org.
# a is a np.ndarray
a.dtype # datatype
a.data # data
a.ndim # number of axes
a.shape # tuple of integers, length of each axes
# if a is a matrix with n rows and m columns, a.shape = (n, m)
len(a.shape) = a.ndim # always true
a = np.arange(15).reshape(3, 5)
# look at the result, list index runs first
6
Creation
x =
np.array( some_list)
x =
np.empty( (2,3) ) # 2x3 matrix
y =
x # Attention this does NOT create a new vector, just a view
y =
np.empty_like( x )# same size as x
x =
np.zeros( (2,3) )
y =
np.zeros_like(x)
y =
np.array(x)
x =
np.random.random(shape=(2,3)) # uniform in [0,1]
x =
np.random.randn(2,3) # normal distributed with mu,sigma
x =
np.random.randint(11,22, (2,3)) # integer uniform in [11,22]
x =
np.loadtxt(filename) # np.savetxt(x, filename) for the other way
x =
np.genfromtxt(filename/url) # great flexibility, for url automatic download
x =
np.arange(a, b, dx) # generates 1D-array equally spaced with step dx=1 by
default
x = np.linspace(a, b, n) # same but with the number of elements
Simple operations
a.T # transpose
a+b # if a.dtype != b.dtype, the more precise will be used (upcasting)
a*b # Attention: elementwise product!
a.dot(b) # matrix product, same as a@b
np.sin(a)
a >= 36 # results in boolean array of same shape
a.min()
a.sum() # if a.ndim=2 (matrix) a.sum(axis=0/1) results in colum/row-wise sum
a.cumsum()
a.mean()
Broadcasting
x = np.random.random((2,3))
y = np.random.random((2,1))
z = x + y # y will be broadcasted to shape (2,3) with identical columns
y = np.random.random((1,3))
z = x + y # y will be broadcasted to shape (2,3) with identical rows
z = x + np.pi # broadcating !
7
Shape manipulation
Functions
a) vectorization
x = np.random.random((2,3))
np.arcsin(x) # applies arcin to all elements of x
b) statistics
Statistics
6 Matplotib
Styles, Markers, Linestyles
• colors: r,b,g,k,w,y,m,c...
• markers:x,X,+,*,o,v,1,2...
• linestyles: -,--,...
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi)
plt.plot(x, np.sin(x), label="sin") # plt.plot(np.sin(x)) plot index against sin
plt.plot(x, np.cos(x), label="cos") # adds another line
plt.legend()
plt.grid()
plt.xlabel("x")
plt.ylabel("y")
plt.show() # not needed in ipython, otherwise never forget...
plt.savefig("plot.png") # format is guessed
8
Simple plots (2)
• Clear
plt.gca().set_aspect(’equal’, adjustable=’box’)
• curve fitting
Different scales
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(-np.pi, np.pi)
fig, ax = plt.subplots()
axt = ax.twinx() # create a twin for different scale
p0, = ax.plot(t, np.sin(t), label="sin", color=’b’)
p1, = axt.plot(t, 100*np.cos(t), label="100*cos", color=’g’)
ax.tick_params(axis=’y’, labelcolor=’b’)
axt.tick_params(axis=’y’, labelcolor=’g’)
#axt.ticklabel_format(axis=’y’, style=’sci’, scilimits=(0,3)) # format if required
#axt.spines[’right’].set_position((’outward’, 0)) # offset if required
plt.legend(handles=[p0,p1]) # needed
9
Several plots (1)
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 10)
plt.subplot(211) #nrows, ncols, fig !
plt.plot(x, np.sin(x), ’bo’, x, -x, ’k’)
plt.subplot(212)
plt.plot(x, np.cos(x), ’go-’, x, x, ’k’)
x = np.linspace(-1, 1)
fig, axarr = plt.subplots(2, 2, sharex=’col’)
axarr[1,0].set_xlabel(’x’)
axarr[1,1].set_xlabel(’x’)
axarr[0,0].set_ylabel(’angle’)
axarr[1,0].set_ylabel(’angle’)
plt.subplots_adjust(hspace=0.6)
plots = axarr[0,0].plot(x, np.arcsin(x), x, np.arccos(x))
plt.setp(plots[0], label="arcsin")
plt.setp(plots[1], label="arccos")
axarr[0,0].set_title("arcsin arccos")
axarr[0,0].legend(loc=’upper right’)
axarr[1,1].plot(x, np.arctan(x), label=’arctan’)
axarr[1,1].legend(loc=’best’)
2D contour plot
x = np.linspace(0, 1)
xm = 0.5*(x[1:] + x[0:-1])
y = np.linspace(-1, 1)
ym = 0.5*(y[1:] + y[0:-1])
xx, yy = np.meshgrid(x, y)
xc, yc = np.meshgrid(xm, ym)
#pseudocolor plot
p = plt.pcolormesh(xx, yy, np.sin(xx*yy), cmap=plt.cm.RdBu, shading=’auto’)
plt.colorbar(p)
# isolines
cnt = plt.contour(xc, yc, np.sin(xc*yc))
plt.clabel(cnt, cnt.levels, inline=True, fmt=’%.1f’, fontsize=10)
# plot contours (uses interpolation)
#cnt = plt.contourf(xc, yc, np.sin(xc*yc))
10
3D plot
fig = plt.figure(figsize=(12,6))
ax1 = fig.add_subplot(1, 2, 1, projection=’3d’)
p = ax1.plot_surface(xx, yy, u0)
ax2 = fig.add_subplot(1, 2, 2, projection=’3d’, sharez=ax1)
p = ax2.plot_surface(xx, yy, u)
def on_move(event):
if event.inaxes == ax1:
if ax1.button_pressed in ax1._rotate_btn:
ax2.view_init(elev=ax1.elev, azim=ax1.azim)
elif ax1.button_pressed in ax1._zoom_btn:
ax2.set_xlim3d(ax1.get_xlim3d())
ax2.set_ylim3d(ax1.get_ylim3d())
ax2.set_zlim3d(ax1.get_zlim3d())
elif event.inaxes == ax2:
if ax2.button_pressed in ax2._rotate_btn:
ax1.view_init(elev=ax2.elev, azim=ax2.azim)
elif ax2.button_pressed in ax2._zoom_btn:
ax1.set_xlim3d(ax2.get_xlim3d())
ax1.set_ylim3d(ax2.get_ylim3d())
ax1.set_zlim3d(ax2.get_zlim3d())
else:
return
c1 = fig.canvas.mpl_connect(’motion_notify_event’, on_move)
11
Animation
c) Save animation
writer = animation.PillowWriter(fps=30)
anim.save(’file.gif’, writer=writer)
12
Images
7 Sympy
Symbolic derivative
expr = "exp(-0.1*x**2)*sin(pi*x)"
f = sympy.lambdify(’x’, expr, "numpy") # makes numpy function
df_expr = sympy.diff(expr, ’x’)
df = sympy.lambdify(’x’, df_expr, "numpy")
8 Scipy
Optimization
f = lambda x: x**10
df = lambda x: 10*x**9
assert optimize.check_grad(f, df, x0=[-1,0,1])
ODEs
import numpy as np
import matplotlib.pyplot as plt
from scipy import integrate
def f(t, y): return [-y[0] + 2*y[1], -y[1]]
t = np.linspace(0, 2)
y0 = [1, 10]
y = integrate.odeint(f, y0, t, tfirst=True)
# Alternative: sol = integrate.solve_ivp(f, [t0, t1], y0, t_eval=t)
# assert np.allclose(y, sol.y)
plt.plot(t, y[:,0], label="y1")
plt.plot(t, y[:,1], label="y2")
plt.legend()
plt.show()
13
9 Pandas
Basics
Pandas is a database library based on numpy, similar to SQL. The basic dataype is pd.DataFrame, it is like a two-
dimensional array, but with varying types, or a dictionary with columns as keys. For each column, we have a row,
which is pd.Series.
import pandas as pd
df = pd.DataFrame({’col1’: [1, 2], ’col2’: [3, 4]}) # creates a DataFrame
df.columns # gets all columns
df = pd.read_excel(filename) # reads excel file
df.to_excel(filename, index=False) # writes to excel file, no index created
Access
Operations
14
10 tkinter
Ingredients
This is the python interface to Tk, one of the first libraries for GUI (object-oriented!). Good reference: https:
//effbot.org/tkinterbook.
A minimal example:
import tkinter as tk
root = tk.Tk() # create the main window
label = tk.Label(root, text="Hello tk")
label.pack() # only packed widgets are shown
button = tk.Button(root, text=’Quit’, command=root.destroy)
button.pack()
root.mainloop() # runs the event loop
Tricks
15