Pattern matching and text manipulation Bram Kuijper

Python for scientific research

Pattern matching and text manipulation

Bram Kuijper

University of Exeter, Penryn Campus, UK

March 3, 2020

Bram Kuijper

Python for scientific research

What we've done so far

1 Declare variables using built-in data types and execute operations on them

2 Use flow control commands to dictate the order in which commands are run and when

3 Encapsulate programs into reusable functions, modules and packages

4 Use string manipulation and regex to work with textual data 5 Interact with the file system 6 Number crunching using NumPy/SciPy 7 Next: Introducing Matplotlib, Python's plotting library

Bram Kuijper

Python for scientific research

Introduction

s1 and s2

coherence

Matplotlib is a 2D and 3D plotting library that produces publication-ready scientific figures in most formats (e.g png, eps, svg)

0.05

0.00

0.05

1.00 0

1

2

3

time

4

0.75

0.50

0.25

0.00 0

10

20Frequency30

40

4 20 2 4

-----1000000001.........0.7153351701916446191 4 20 2 4

5 50

0.75 0.50 0.25 0.00

0.25 0.50 0.75

ylabel ylabel

violin plot

20

20

10

10

0

0

10

10

20

20

x1

x2

x3

x4

x1

xlabel

9

8

7

6

5

4

3

2

1

4

2

0

2

4

box plot

x2

x3

x4

xlabel

1.5

1.0 102

0.5

0.0

101

0.5

1.0

1.5

100

2.0 3

A line plot o9n0?a polar axis

135?

45?

180?

0.5

1.0

1.5

0? 2.0

225?

315?

270?

Simp0le.0s0t0default with labels

0.800

0.400 11.2.60000

-1.200

0.000

-0.800

-0.400

2

1

0

1

2

Bram Kuijper

Python for scientific research

Anatomy of a figure

4 Major tick

Minor tick 3 Major tick label

Anatomy of a figure

Title

Blue signal Red signal

Legend

Grid (linLeinpelot)

Y axis label

2 Y axis label

1

(scMatatrekreprslot)

Figure

Axes (linLeinpelot)

Spines

0

0 0.25 0.50 0.75 1 Minor tick label

1.25

1.50

1.7X5axis2

2.25 label

X axis label

2.50 2.75 3 Made with

3.25 3.50 3.75 4

Bram Kuijper

Python for scientific research

My first plot

1 import numpy as np 2 import matplotlib.pyplot as plt

3

4 # Generate sinusoidal data 5 x = np.linspace (0, 4*np.pi , 100) 6 y = np.sin(x)

7

8 # Plot sinusoidal curve 9 plt.plot(x, y, color="blue") 10 plt . xlabel ( " Time " ) 11 plt . ylabel ( " Amplitude " ) 12 plt . title ( " My first Python plot " )

Amplitude

My first Python plot 1.00

0.75

0.50

0.25

0.00

0.25

0.50

0.75

1.00

0

2

4

6

8

10 12

Time

Bram Kuijper

Python for scientific research

Subplots

1 plt.subplot(2, 1, 1) # 2 rows , 1 column , first plot 2 plt.plot(x, np.sin(x), color="b", label="Sine") 3 plt.ylabel("Amplitude") 4 plt.legend(loc="upper right")

5

6 plt.subplot(2, 1, 2) # 2 rows , 1 column , second plot 7 plt.plot(x, np.cos(x), color="r", label="Cosine") 8 plt.xlabel("Time") 9 plt.ylabel("Amplitude") 10 plt . legend ( loc = " upper right " )

Amplitude

1.0

Sine

0.5

0.0

0.5

1.0

0

2

4

6

8

10 12

1.0

Cosine

0.5

0.0

0.5

1.0

0

2

4

6

8

10 12

Time

Amplitude

Bram Kuijper

Python for scientific research

Histograms

1 # Generate 1000 normally distributed numbers 2 x1 = np.random.randn (1000) 3 x2 = np.random.randn (1000)

4

5 # 1D histogram (x1) 6 plt.subplot(1, 2, 1) 7 plt.hist(x1 , bins =20)

8

9 # 2D histogram (x1 vs x2) 10 plt . subplot (1 , 2 , 2) 11 plt . hist2d ( x1 , x2 , bins =20) 12 plt . colorbar ()

120

100

80

60

40

20

03

2

10

1

2

3

3

20.0

2

17.5

1

15.0

12.5

0

10.0

1

7.5

2

5.0

2.5

3

2

10

1

2

3

0.0

Bram Kuijper

Python for scientific research

Boxplots and violin plots

1 # Generate four sets of random numbers 2 data = [np.random.randn (1000) for i in range(4)]

3

4 # Boxplot 5 plt.subplot(1, 2, 1) 6 plt.boxplot(data)

7

8 # Violin plot 9 plt.subplot(1, 2, 2) 10 plt . violinplot ( data )

4

3

2

1

0

1

2

3

41

2

3

4

4

3

2

1

0

1

2

3

41

2

3

4

Bram Kuijper

Python for scientific research

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download