hello and welcome back to lesson number
six
uh for this lesson i'm going to be
talking briefly about glob which is an
incredibly useful package of one of the
best names of packages in the python
ecosystem
and i'm also going to talk a little bit
about matplotlib and some more advanced
things you can do with it
okay so first of all glob what's glob
well it's a package that comes with
python so you should all be able to
install it without having to use pip
install
you import it like so and it just has
one function that we're going to use
which is called glob
glob.glob
what this does is it pattern matches
with file names in
your current directory by default but
you can look in other directories as
well so we can
tell it to tell us anything that ends
with txt
we're using wildcards here much like
they are used in the ls command in bash
for those of you that have experience
with linux or mac
what this will then do is it will fetch
all of the files in our current
directory or in the given directory that
end in dot txt and return them as
separate strings in a list
to the user so we can say
all
sim
files
okay cool
that's great so what do we do with it
well now that we've got this information
we can use the matplotlib plotting
skills we've got to plot all of the
simulation files on a single plot to see
just how much they vary
so first of all we're going to import
matplotlib dot pi plot fplt
we're going to need to pass the data so
we're going to use numpy as well
and then we're going to say for sim
file in
all sim files let's create a for loop
we're going to
[Music]
add in some other things but just before
we do that and before i forget
we need to remember to turn on
interactive mode for the macbook lip
plot
now let's create a figure
let's create some axes on that figure
just as i've shown you so far you can
see this is what the figure looks like
now
all i need to do
is write this for loop so for sim
file in
all
sim files
you can say
the data is equal to
np dot load text
and then we just provide the sim file
name
and
skip rows of 52
now we've got x is equal to
same all the rows first column
y is equal to some all the rows second
column
and z is equal to
sim
all the rows
third column
now we've got all the data so now we
just need to do a cross section through
it
so we do that by finding the smallest y
value so what we can do is say okay
x is equal to x where y is equal to
np
dot amin so we're taking the smallest
value of y
well the lowest value of y after
making sure that we've absoluted it
then we can do the same thing with z
and
with that we have extracted the cross
section for the luminance in every
simulation file and now all we need to
do is x dot plot
x on the x-axis
luminance on the y-axis and let's put a
label in as well i'm going to say label
equals and let's just call make sure
that each one
is labeled with the simulation file
there we go let's execute that for loop
now you can't really see it because it's
not on the screen but there we go the
figure has now plotted every single one
but which one is which how do we know
well that's why we've included this
label
because the label will correspond to the
file name or we need to call it
ax.legend
and we don't need to provide any
arguments but i'm going to say location
equals
best which will automatically put the
legend in the best location that covers
the least amount of data
and if we look back at it
we can see
we've got a legend up here in the top
right now
it tells us which files belong to which
this is pretty powerful as i'm sure you
can imagine
now the thing is we can actually do a
bit more with this like this is great
and all but we can do quite a bit more
to our matplotlib plot things that we
haven't really covered so far
for example
one thing we can do is set the labels
so we can say x dot set
y label
and then we can provide a string it's
going to be luminance
and the units
are candela
per meter square
so i'm going to use
latex here for those of you that know
how this works
and that is cadmium candela
meters to the power
of minus two
close square brackets close quotes
close brackets
and this luminance
candela per meter square will now appear
in math format on the image so if we
look at this
you can see it's a bit hard to see here
if we expand it you can see we've got
superscripts going on and everything is
italicized this is quite useful
i can do the same thing for the x label
position
millimeters
in fact we want to probably make clear
that this is
x position so
x position
millimeters
close brackets
now if we look back to our plot we've
added an x label here as well
we've got the y label and the x label
now we just need a title
set title
what we say
probably that this is a
luminance
cross section
plot
there we go and now we have set the
title on our plot
this is quite powerful
we're building up quite a lot of things
regarding our plot that we didn't have
before in fact
we're probably quite happy with
with this we're basically done so what
we can do is then save it from
config.fig
and then we provide some kind of file
name so
my
my multiple
line
graph
dot png
then that will save the figure in a
folder which we can then open so i'd
recommend you try that yourselves
we can also call it exactly the same
thing as our
simulation file it's quite powerful
but basically we've got a lot of things
going on that we can
augment our plot with
and that really is it
and that really is it there's quite a
lot you can do in matplotlib there's
quite a bit more in the notes about
things that we do and we show you can do
so have a go at the exercises
now note that these are exercises are a
little special
because they're actually fairly
straightforward but they require a lot
of
uh creativity from how to treat them
you're not expected to just go through
the notes and rewrite everything exactly
it's more of an expectation that you
will go through and use the materials
that you have available to you i can
guarantee that the first exercise can be
finished in under a minute if you're
quick
just need to make use of the notes that
are in the exercises and what they
already do
so please take some time to go through
those make sure you understand what
they're doing before you leap into the
exercises
all right i'll see you in lesson seven
you