The code examples for this tutorial are currently only given for Python/Numpy. Conversion to Matlab is not difficult, but would be confusing for beginners (who are the target audience here). I may get round to doing the extra work at some point soon.
This tutorial is aimed at absolute beginners. Ideally get an experienced person to setup Python* for you because it is liable to be an off-putting hurdle otherwise. (When you are an expert yourself, you will have plenty of opportunities to repay your debt to the world!)
Before we get to the actual tutorial, you need a tiny bit of preparatory code that you don’t need to understand. Copy-paste the following few lines into your interpreter/script (ask your nominated “experienced person” how you do this, and then keep them around for another minute or so just to check that everything is working as it should):
def show_flag(data, colors=['#ff0000','#ffffff','#0000ff'], title="my flag", fignum=1): from matplotlib.colors import ListedColormap from matplotlib.pylab import matshow, gca, clf cm = ListedColormap(colors) if fignum is not None: clf() matshow(data, cmap=cm, fignum=fignum) gca().axis('off') gca().set_title(title)
That function will let us display 2D arrays as colorful flags. Here is a very simple example to start with:
flag = [ [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 2, 2, 2], [0, 0, 0, 0, 2, 2, 2] ] show_flag(flag)
Notice how the flag contains just the numbers
0 1 2. Inside the
show_flag function these numbers are converted to colors (rather like the color-by-number pictures you may have done as a child). By default,
0 maps to red,
1 maps to white and
2 maps to blue. (I chose those colors because they’re pretty common for flags. I’ll show you how to customize the color mapping later on.)
Right, there’s one more thing you need before we get started:
# Use one (or both) of the following two lines: from numpy import * # simple, but frowned upon import numpy as np # better, but sometimes annoying
I will explain this briefly because it’s worth understanding, but I’ll have to use “scare-quotes” a bit as the explanation is rather hand-wavy: basically, Python is not a good language for doing heavy number crunching. If you try adding/subtracting/multiplying individual numbers thousands/millions/billions of times there will be a huge amount of “overhead” and you will get very slow (and complicated-looking) code. However, Python does allow, and encourage, the use of “plugins”. These plugins are free to do special kinds of “magic behind the scenes”, so long as they present a “Python-friendly exterior” that the user (i.e. you) can interact with in Python.
Numpy is one of these “plugins”, but it’s a rather special one in so far as it provides such significant “number-crunching functionality” that no other plugin can really compete with it. If you want to do number crunching in python you basically *need* to use Numpy. Most plugins that provide advanced number-crunching tools build on top of Numpy rather than replace it. (Not all plugins are about “number-crunching”, they do all kinds of things, like helping generate HTML for webpages etc.)
So now I can explain
import numpy as np. This says to Python: “I have a plugin called ‘numpy’ and I’d like to use it here please. And I would like to refer to it as ‘np’ for short.” You can then use commands like
np.ones(40), and python will “delegate” to Numpy to do its magic. If you use the alternative form
from numpy import * then you are saying to python: “I have a plugin called ‘numpy’ and I’d like to use it here please. And I’m not going to tell you explicitly when I’m trying to use it – if I mention something from inside numpy, assume that is indeed what I meant.” This second version is sometimes easier because you don’t have to type
np. all over the place, but it can also confuse your text editor and may introduce subtle bugs or confuse readers of your code. To keep the code in this tutorial compact, I’ll use the
from numpy import * version.
Ok, finally we are ready to start…
The French Tricolour
H, W = 50, 70 # remember that our defaults are: red=0, white=1, blue=2 flag = zeros([H, W]) flag[:, :W/3] = 2 flag[:, W/3:2*W/3] = 1 show_flag(flag, title="Vive la France")
Try it out and check that it works. Can you make sense of the code?
We begun by creating a 2D array of shape
50 x 70. In numpy we refer to the dimensions of a multidimensional array as “axis 0”, “axis 1”, “axis 2”, “axis 3” etc. Here axis 0 is of length 50 and axis 1 is of length 70. You can see in the flag that the 50 and 70 correspond to the vertical and horizontal dimensions respectively (because the image of the flag is wider than it is tall). The array we started with is all zero, which gets mapped to red when we display it. There are other functions for initializing arrays but this is all we need for now.
flag[:, :W/3] = 2 is setting the blue section of the flag. It does this by indexing into the whole of axis 0 (the vertical), but only the first third of axis 1 (the horizontal).
The next line
flag[:, W/3:2*W/3] = 1 is similar, setting the white portion of the flag. Here the relevant section spans from the end of the first third, to the end of the second third.
That was all we had to do in this example.
Vlag van Nederland
Have a go at doing this yourself. Note that you may have to guess a little bit for some of the syntax.
Expand to view my suggested solution…
H, W = 50, 70 # remember that our defaults are: red=0, white=1, blue=2 flag = zeros([H, W]) flag[H/3:2*H/3, :] = 1 flag[2*H/3:, :] = 2 # set last third to blue show_flag(flag, title="Van Gogh would love numpy")
This looks pretty similar to the French version, but we’re indexing into axis 0 rather than axis 1. Note that here we don’t need to set the first third to blue, but we do need to set the last third. Hopefully you were able to guess the necessary syntax.
Les Quatre Bandes of Mauritius
You should be able to make the array for this yourself, but I need to tell you how to customize the color mapping:
show_flag(flag, title="What's the capital of Mauritius?", colors=['#00A551', '#EA2839', '#1A206D', '#FFD500'])
I found the four color codes using the colorpicker in Chrome devtools – google for help if you’ve never done that kind of thing.
My solution looks like this:
H, W = 50, 70 flag = zeros([H, W]) flag[:H/4, :] = 1 flag[H/4:2*H/4,:] = 2 flag[2*H/4:3*H/4,:] = 3 show_flag(flag, title="What's the capital of Mauritius?", colors=['#00A551', '#EA2839', '#1A206D', '#FFD500'])
There shouldn’t be anything new here really. But if you want to get obsessive you can spend more time fiddling with the exact proportions.
My solution looks like this:
H, W = 100, 140 flag = zeros([H, W]) flag[:, W*0.3:W*0.48] = 2 flag[H*0.33:H*0.56, :] = 2 flag[:, W*0.35:W*0.44] = 1 flag[H*0.39:H*0.5, :] = 1 show_flag(flag, title="How deep is your fjord?", colors=['#EF2B2D', '#002868', '#ffffff'])
Note that I doubled
H to increase resolution a bit. Also note that we are now using non-integer indices for the arrays, or rather we are not explicitly using integers: numpy will round our floating point numbers down to the nearest integer before doing anything with them.
The Jamaican Cross
How are we going to deal with those diagonal lines? Well, hopefully you already are familiar with the basic equation of a straight line
y = mx + c. Here we are essentially going to use that, but we’ll treat it as an inequality rather than an equality, i.e. for a given
y at a specific
x horizontal position, we want to know is it above or below the line? The code is now a bit more complicated:
from numpy import newaxis as nax H, W = 100, 140 x = arange(W)[nax, :] y = arange(H)[:, nax] flag = y > x*0.75 - W*0.1 show_flag(flag, title="Everything's gonna be alright", colors=['#000000', '#FED100']) # we'll also need '#009B3A' (green) later
The first line is just a convenient way to shorten the word
nax, we can then use it on the third and fourth lines:
arange(W) creates a 1D array of the numbers
[0, 1, 2, ..., W-1]. When we put
[nax, :] after that, we are changing the shape of this 1D array to make it a 2D array of shape
1 x W. This means we can treat the
x array as a single horizontal “row” in the flag, giving the x-coordinate of each column. Equivalently, the
y array is a column, giving the y-coordinates of each row. Once we’ve setup these two variables, we are able to combine them in interesting ways.
By “setup” I mean that we’ve created two arrays with the same number of axes (i.e. 2), and the axes lengths “agree” with each other. What does it mean for two arrays to have axes lengths that “agree”? Well, it means specifically that each axis either has a length of 1, or it has exactly the same length as in the other array. For example
10 x 1 x 17 agrees with
10 x 23 x 1, but it doesn’t agree with
17 x 1 x 10. Once we have agreement, numpy can automatically “broadcast” any operations (such as adding, subtracting, comparing) out to match the full length in all axes.
TODO: a diagram of broadcasting would help a lot here
So here, we broadcast out the row
x and the column
y with the “greater-than” operation, and the result is a 2D array of
falses, telling us which elements in the array are above/below the line. When we display the flag the
trues are converted to 1s and the
falses to 0s (and then the 0s and 1s are mapped to colors as with previous flags). Note (if you’d hadn’t already) that the 0-end of the vertical axis is at the top of the flag and the H-end is at the bottom. If you want to flip this round you can add the line
gca().invert_yaxis() at the end of your
show_flag function (and then re-evaluate the function to update it).
We are not done yet, but hopefully you can now see how you might go about constructing the flag. If you get stuck, don’t feel too bad jumping to the suggested solution as I haven’t explained everything that you’re going to need to use here.
from numpy import newaxis as nax H, W = 100, 140 flag = zeros([H, W]) x = arange(W)[nax, :] y = arange(H)[:, nax] yellow_part = ((y > x*0.75 - W*0.1) & (y < x*0.75 + W*0.1)) flag[yellow_part] = 2 yellow_part = ((y > W*0.6 - x*0.75) & (y < W*0.8 - x*0.75)) flag[yellow_part] = 2 green_part = ((y >= x*0.75 + W*0.1) & (y >= W*0.8 - x*0.75)) flag[green_part] = 1 green_part = ((y <= x*0.75 - W*0.1) & (y <= W*0.6 - x*0.75)) flag[green_part] = 1 show_flag(flag, title="Everything's gonna be alright", colors=['#000000', '#009B3A', '#FED100'])
Right, that’s a little bit messy, but it’s largely unavoidable. There are two new things here.
Firstly, we are using
& to combine pairs of 2D arrays: the combined array has the same shape as original two arrays (numpy would let us use broadcast again here if we needed to) and its elements are
true where the elements of both arrays were
false elsewhere. With this mechanism we are able to say: “I want the band where y is greater than something *and* less than something else.“.
The second new thing is that we are indexing into
flag using a 2D array of
falses rather than bands of indices. In addition to these two methods of indexing there are one (or two?) more that we haven’t covered yet.
Nisshōki of Japan
falses like we did in Jamaica, but now we need to create a circle.
from numpy import newaxis as nax H, W = 100, 140 flag = hypot(arange(-W/2, W/2)[nax, :], arange(-H/2, H/2)[:, nax]) > 30 show_flag(flag, title="Pythonic haiku? Not really.", colors=['#ff0000', '#ffffff'])
I Galanolefki of Greece
Although you could just use the basic stuff we learnt at the start, here we create all the stripes with a single command.
H, W = 100, 140 flag = zeros([H, W]) is_stripe = arange(H) % 22 > 10 flag[is_stripe, :] = 1 flag[:55, :50] = 0 flag[:55, 20:30] = 1 flag[22:33, :50] = 1 show_flag(flag, title="'flag' is not of Greek origin", colors=['#0D5EAF', '#ffffff'])
* I use the Spyder IDE as distributed with anaconda, but you could possibly use an IPython notebook if you prefer. The only modules we need for this tutorial are Numpy and Matplotlib. I use Python 2.7, but everything here is probably more or less fine in 3.x.
This tutorial took a good while to write, what with all the explanations of how to use numpy and the actual messing around to make the flags. If you found it useful let me know in the comments. If I have time I may add some more examples to deal with stepped indices, reverse indices (maybe?), and perhaps suggest how custom-written functions can be used to draw complicated shapes like circles or stars…and then those functions can be reused for later flags.