Plotting arrays

This notebooks shows some possible ways of displaying two-dimensional data. The aim is to transform the shapes below into data and visualize this data as shapes again.

Armin Hofmann - Cells

We will interpret the shape as a 3 x 3 grid with white and black cells. A white cell is represented by a 0, a black cell by a 1.

Variables and Functions

Create a list by hand

First we’ll transfer the top left shape of the image above into a list.
A list in Python is defined through square brackets: [].
Items inside the list are separated by commas.
A list can contain any kind of data, as well as it can contain other lists.
For the grid above we’ll create a list which contains 3 lists, one for each row.

data = [[1, 1, 0,],
        [0, 1, 1],
        [0, 1, 1]]

A very useful and powerful concept of programming is the variable.
A variable defines a space in memory where data is stored.
Variables are initiated through a name of your choice (see the appendix at the bottom for more details) followed by = followed by the data you want to store in it.
The list is stored in a variable called data.
We can access the data through the name of the variable, which is like an address.
Through variables, data becomes reusable and code becomes flexible.

For example we can feed it into a function called print() to display it inside this notebook:

print(data)
[[1, 1, 0], [0, 1, 1], [0, 1, 1]]

We can identify a function through the () behind the name of the function.
If we want to insert data into a function, we can simply put it inside the brackets.

Plot an array (list) with matplotlib

Imports

Python comes with a huge amount of built-in capabilities. Nevertheless the real power lies in the thousands of external libraries that we can include and make use of the code. So we don’t have to write everything on our own, instead we can use code that other people thankfully wrote for all of us.

We’ll deal with libraries in more detail later, for now it’s important to see that we import a library called matplotlib in our program.

Commonly this is done at the beginning of a program. At least a library needs to be imported one step before it’s used in the code.

# If matplotlib is not installed (or you don't know), run
!pip install matplotlib
import matplotlib.pyplot as plt

# feed the data into a function called imshow
plt.imshow(data)
# show the plot
plt.show()
_images/plotting_arrays_9_0.png

Adjust the design

from matplotlib import colors

cmap = colors.ListedColormap(['white', 'black'])
plt.figure(figsize=(4,4))

plt.pcolor(data[::-1],cmap=cmap)

# exclude ticks
plt.xticks(ticks=[])
plt.yticks(ticks=[])

plt.show()
_images/plotting_arrays_11_0.png

Create random data

Next we’ll shuffle the rows of our list with a method called shuffle() from the external module random:

import random

random.shuffle(data)
print(data)
[[0, 1, 1], [0, 1, 1], [1, 1, 0]]

Define own functions

Next we want to display the shuffled data. For that we’ll use the code from above. One way is to copy everything. This may be easy, but the disadvantage is that it’s not easy to reuse it (we may have to copy it over and over again) and if we want to make changes to the plotting function, we have to change it everywhare.

Instead of doing this it’s recommended to work with own functions. It’s very easy, we just have to copy the code (from above) into a definition statement:

def plot_data(data):
    import matplotlib.pyplot as plt
    from matplotlib import colors
    
    cmap = colors.ListedColormap(['white', 'black'])
    plt.figure(figsize=(4,4))

    plt.pcolor(data[::-1],cmap=cmap)

    # exclude ticks
    plt.xticks(ticks=[])
    plt.yticks(ticks=[])

    plt.show()

Everything inside the function is indent by one tab / 4 spaces.

Next we can call this function and insert our data as an argument into it:

plot_data(data)
_images/plotting_arrays_18_0.png

Create random data with numPy

random sample: https://numpy.org/doc/stable/reference/random/generated/numpy.random.randint.html#numpy.random.randint

import numpy as np

data = np.random.randint(low=0, high=2, size=(3,3), dtype=int)
print(data)
[[1 0 1]
 [0 0 1]
 [1 1 1]]
# Display the data
plot_data(data)
_images/plotting_arrays_21_0.png

Save a plot

Docs: https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.savefig.html

You have to call the function .savefig() before calling .show(). The latter will clear the plot. See https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib.

from matplotlib import pyplot as plt
from matplotlib import colors
cmap = colors.ListedColormap(['white','black'])
plt.figure(figsize=(8,8))

# include edges
plt.pcolor(data[::-1],cmap=cmap,edgecolors='k', linewidths=1)

# exclude ticks
plt.xticks(ticks=[])
plt.yticks(ticks=[])

# Save the plot before executing .show().
plt.savefig('matplot.png')
plt.show()
_images/plotting_arrays_23_0.png

Appendix

Names and naming conventions

Names of variables and functions (like len()) must start with a character (not a number), but can contain also numbers.
In Python it’s common to separate words inside a name with a underscore. But this is just a convention and not a must. The actual name is of our choice, but we should avoid using the keywords of the language itself or the names of existing functions. Syntax highlighting helps us to avoid this.

# examples
txt_length = 4  # ok
txtLength = 4  # ok but not python style
txt_1 = 4  # ok
txt1 = 4  # ok
1txt = 4  # not ok
txt-length = 4  #  not ok as it's an expression (the variable txt minus the variable length)
print = 4  # possible but not recommended, because it (temporarily) overrides the print() function