` 12th bite of Python

Python XII

Mary PW Chin 钱碧慧博士
PhD (Wales), MSc (Surrey)
E-mail:

1st bite of Python

Printing

2nd bite of Python

Loops

3rd bite of Python

If-elif-else

4th bite of Python

Nested loops

5th bite of Python

Input, while, tables

6th bite of Python

Strings

7th bite of Python

Simple maths

8th bite of Python

More loops

9th bite of Python

Dictionaries

10th bite of Python

Drawing

11th bite of Python

Statistics & histograms

12th bite of Python

Pretty nets

13th bite of Python

Medical scans

14th bite of Python

Handwriting recognition

15th bite of Python

Flower recognition

Plotting nets

Pretty nets

We shall plot out some pretty plots -- each consists of mathematically staggered straight lines, arranged using different number sequences. A beautiful showcase of loops and number sequences. Nothing but straight lines!

Here are the ingredients you need. Let us say we have two points. One point at coordinates (x,y) = (x1,y1). Another point at coordinates (x,y) = (x2,y2). To plot a straight line joining the two points, we use:
import matplotlib.pyplot as plt
plt.plot( [ x1, x2 ], [ y1, y2 ] )
plt.savefig('myfilename.png')

As an example, if we want to plot a straight line joining point (3,5) to point (17,2), we shall have:
plt.plot( [3, 17], [5, 2] )

Pretty nets with a square frame

Where do we begin?

  1. Divide the plot into 4 banks:
    • lines extending from points along x=0 to points along y=0;
    • lines extending from points along x=50 to points along y=0.
    • lines extending from points along x=0 to points along y=50;
    • lines extending from points along x=50 to points along y=50;
  2. For each bank, construct a 4-column table, with each row on your table corresponding to one straight line of the bank.
    x1y1x2y2
  3. The 'manual' way of plotting would be to have a line
    plt.plot( [ x1, x2 ], [ y1, y2 ] )
    for each of the line we wish to plot. This is the first step.
  4. The next step would be to put the above into 4 loops. A loop for each bank.
  5. The next step would be to condense the 4 loops you already have into a single loop containing 4 lines of
    plt.plot( [ x1, x2 ], [ y1, y2 ] )

Pretty nets with a circular frame

This will require some trigonometry, as presented in the slides.

Pretty nets with a triangular frame

This will require some trigonometry, as presented in the slides.

Exercise Write a program to take as input the coordinates of two dots (x1,y1) and (x2,y2), then calculate the gradient (m) and interception (c) for a straightline y = mx + c joining the two dots. Do three versions:
  1. where the input is hard-coded;
  2. where the input is given as runtime arguments;
  3. where the input is given interactively during runtime.
(You may refer to '6th bite of Python' slide #24.)

1st bite of Python

Printing

2nd bite of Python

Loops

3rd bite of Python

If-elif-else

4th bite of Python

Nested loops

5th bite of Python

Input, while, tables

6th bite of Python

Strings

7th bite of Python

Simple maths

8th bite of Python

More loops

9th bite of Python

Dictionaries

10th bite of Python

Drawing

11th bite of Python

Statistics & histograms

12th bite of Python

Pretty nets

13th bite of Python

Medical scans

14th bite of Python

Handwriting recognition

15th bite of Python

Flower recognition