Containers
Despite getting rid of y0
and x0
we haven't muched reduced our code yet (we'll look at
this in the next practical). What we have done, however, is put our data in a much stronger form for analysis.
In order to analyse data simply, without building analysis toolkits ourselves, we can use pre-existing code.
This comes in three forms: built in functions, which are automatically in Python when it starts up; functions in modules
(or "packages" – the two are roughly the same) from the standard library collection that is distributed
with Python, or functions in modules that are distributed separately (Anaconda comes with many). The latter two are
accessed through the import
keyword, as we've seen in the case of the standard library
random
module.
This section will introduce one of each of these three options, and give examples of their use. Although we'll talk about how they are used, don't worry about this so much now; we'll cover functions a little later in the course and the specifics will become clear. For now we'll give you enough code to introduce the modules and functions themselves.
As an example of the strength of having our data as a list, let's run an analysis to work out
which agent is furthest east (larger x
). To do this, we
can use the built in function max()
. Let's say we run:
print(max(agents))
max()
will run through the list of agent coordinates, and work out which is largest. Try it out.
Remember, our list looks something like this (with random numbers):
[
[34,22],
[27,51]
]
You should see that the answer depends on the first, y
, figure in each pair of coordinates, only
taking the x
into account if all the y
are equal (how might you test this?). Given this, from the above it would therefore return:
[34,22]
So how do we access the second, x
, number in preference to the y
s?
Many functions will take in additional arguments (the values passed into the function
between the parentheses). We've seen, for example, that the print
function can take in
multiple arguments: print(y0, x0)
. Extra arguments are comma separated; how many you can use
depends on the function. Stranger
still, functions can take in other functions as arguments.
We'll see how this works later in the course;
for now, we can pass an additional function into max
which will be used to determine the value to look for
when assessing size.
The other thing you need to know is that some arguments to functions need to be named, in this form:
function(name=variable)
These are known as keyword arguments or kwargs. Again, we'll deal with these later in the course. For now, the important thing is that there's a function that will extract the second element of a list for us.
To use it, we'll have to import the operator
package. Here's the code:
import operator # Do this line at the top of the code.
print(max(agents, key=operator.itemgetter(1))) # Do this line at the bottom.
When the max
function gets each element of the agent
list, each of which is a list as well,
operator.itemgetter(1)
gets the second element (remembering that containers are indexed from zero).
Copy the code into the bottom of your file and run it.
You should see that the code now gives us the max in the x direction.
Here we've used a built in function (max
) and a standard library function
operator.itemgetter
. Let's now look at an external library: matplotlib.pyplot
.
matplotlib.pyplot
is used to plot data in graphs. Here, we're going to use it to plot our
agent locations.
Import matplotlib.pyplot
into the top of your code. Then copy the following code into the bottom of your file:
matplotlib.pyplot.ylim(0, 99)
matplotlib.pyplot.xlim(0, 99)
matplotlib.pyplot.scatter(agents[0][1],agents[0][0])
matplotlib.pyplot.scatter(agents[1][1],agents[1][0])
matplotlib.pyplot.show()
If you're interested, you can see the documentation for pyplot on the matplotlib site. If you run this in Spyder, the output will go to the console. If you run it at the command line, you'll see a popup window.
What would be good would be if we could colour the agent furthest east a different color. You know most of what you need to do this. The only additional info that would be useful is that:
- to colour a point, use
matplotlib.pyplot.scatter(x-coordinate, y-coordinate, color='red')
, before theshow
command; - remember that the max function returns a sublist from our agents list, e.g.
[27,51]
. - and that if you plot a point twice, the last imposed colour is used (though this may or may not be useful, depending on how you do it).
Give it a go getting that working, and then we're done for now. Next practical we'll add some key control flow, dramatically reducing and enhancing the code we've written.