Hello everyone and welcome to the chloroplast map's lecture and this lecture we're going to go over
how to use Python and partly to create coral maps which will allow us to plot out information either
on a global scale or any nation wide scale.
Let's go ahead and jump to Jupiter notebook to get started.
All right.
Here I am at the Jupiter note but the Supernaut books called Kuro plus maps.
And you can find under Python for data visualization under the geographical plotting folder and you
should start in the note book or work inside of this book in order to access the V data.
We're going to be working with again all the code we're going to be typing up as a reference here.
And also he should know that there's references for his explanations plus plots can be really difficult
at first especially the syntax used for plotless.
So you'll always want to keep this note book as a reference for things.
And we're going to do just go out and explain the code in this notebook see some simple examples at
first using just nation wide scale plots then we're going to see some real data do a larger plot on
nationwide scale and then we're going to go ahead and see the world scale so we can actually plot on
two countries.
All right let's go ahead and start a new chapter notebook.
All right here I am at a Jupiter note book I'm going to go ahead and start up with some imports.
First I'm going to import slightly dot plot Ali as he y.
And then I'm going to say in order to do all of this off line or save from plot the off line imports
download underscore plot Legia.
And I'm just using tabbed to autocomplete this.
So I just type in it and then it'll autocomplete in it underscored notebook mode and then plots and
plot.
So for functions to complete and you can just copy and paste this code from the notebook if you want
.
And then I'm also going to then call it underscore notebook mode and passen connected is equal to true
.
And that we can see everything inside the Jupiter notebook and there's a link in your lecture notebook
for other options use partly off line.
OK.
Again like I mentioned plot Lee's mapping can be a bit hard to get used to.
So there's a copy of a cheat sheet as well as a link to it in the Jupiter notebook and it looks something
like this.
So you can see basic charts with partly how to use them.
And if you scroll down you can later on see actual maps and the cheat sheets here.
This is kind of the format we're going to be following.
Let's go ahead and jump back to the Jupiter notebook.
First thing we need to do is begin to build our data dictionary.
And the easiest way to do this is using the DCT method.
So you can actually cast a dictionary with the ICC.
So we're going to be using that method to cast the list into a dictionary and the first thing we're
going to do is start off with our data.
So you have your data and it's going to be a dictionary and we're going to have the key type be Coro
Plath.
And then we're going to have locations and locations is a list of states and it's the abbreviation for
the states in our case since we're doing this just on the nationwide scale.
So go in and say Asie CA and NY you can go ahead and put in any postcode abbreviations you want there
.
Then we're going to say location mode.
And in this case we're going to put in USA cash States and this lets Whitely know that we're doing this
on the United States level and there's various location mode and you can go ahead and check out the
links in the Jupiter notebook to get the documentation on the various location modes you can provide
.
Then we're going to go ahead and set the color scale to be equal to Portland which is just a color scale
and we can also do other things such as greens or grays and walk.
We'll mix this up and see how it works later on.
I'll just choose Portland and then we're going to go ahead and do is passen in key text and text is
a list of what hovers over each of the locations.
So I'm just going to put some basic stuff here.
We'll go in and say text one text 2 and then text 3
and then Z is going to be equal to your actual values that are going to be shown in a color scale.
We'll go ahead and just set these two one two and three and I'll put them in as floats.
And then finally pasan a color bar arguments.
And this is actually a dictionary with the key title and then you would just person Whoops you're passing
whatever you want the color bar title to be.
So as a very generic one I'll say color bar title goes here.
Right so that's what data looks like and we just take a look at data.
It's going to be a dictionary for whatever reason the partly documentation likes to use this sort of
dictionary casting call instead of just building it out as a dictionary.
This kind of saves you time for typing and all these quotes and now having to make all these strings
for keys.
So I'll just go ahead and keep in line with the documentation and use this sort of notation.
Next we want to do is create a layout object or variable and layout is going to go ahead and be a nested
dictionary.
And we just passen G.O. is equal to and then we set the scope
to be US A.
And then what we're going to go ahead and use is the go figure.
And in order to do that we're going to need to import something else will have to say import Clottey
dot graph underscore RBJ s as.
And I'm going to go ahead just put this up at the top with the other imports and run that.
OK so no that's important.
I can say Go figure
and set data.
And this is kind of the weird part equal to in brackets so as the item of the list data which is kind
of strange because it's already a dictionary but you pass it in as a dictionary inside of a list and
then you just say lay out is going to be equal to lay out.
You don't technically need to actually passen data equals and laya equals we could just pass in data
and lay out.
Well let's go ahead and just keep it this way and we'll set this whole thing equal to Koro map and then
we do say I plot Curle math.
And we should see the plot here and here we have Arizona California and New York highlighted with the
Portland color chart chosen here.
Now we can go ahead and change it to one of the predefined strings shown in your notebook.
For instance you can go out and change this to jet.
See what changes happen there.
Run everything again.
And here we have now the jet color scheme or we can do other things such as greens is another one you
can choose.
Run that and everything is in green So if New York being the very light green over here let me go ahead
and just break down everything we just did because the formatting can be kind of confusing and it really
it just takes time.
You usually don't actually memorize this.
You'll have to reference examples from the documentation or from the cheat sheet.
But the basic idea is we have our imports we initialized notebook mode and then we have two main objects
a data object and a layer object and I can call these just my variables my data variable and my layout
variable and the past goes into the Go figure function.
And then I want to pass that thing into plot.
If I just pass into plot what will happen is this will open a new tab as an h m file and you can go
ahead and save the say e-mail file or export it or you can download it as a PNB.
All right.
Well let's go ahead and go back to lecture here.
In our data object we have a couple of arguments we have type which clarifies what type of geographical
plot we're doing.
In this case we're focusing right now just Court plots.
Then you have locations locations is an array or a list of those actual state abbreviation codes then
you specify a location mode.
In this case since we're working in USA we're going to say USA Dasch States and this can go down to
county level.
You can go in and reference the documentation that's linked in the chip in a notebook.
For more information on that then you have the text and the text is an array or a list of the text that
lines up for each of the locations.
So for example go out and change this to Arizona change this one to Cali and change this one to New
York and if we run this again and notice that in plot let me go to him.
Let me go to specify it as I plot so he see inside that you put in a book you'll notice when I say I
plot the text value says Kalli Arizona and New York.
So that means that this text just has to be in the same index sequence as the locations and it's going
to match up then the z is whatever value you want to represent as a color.
So for instance Cali was valued to Arizona as value 1 in New York Cazayoux three.
Later on we'll see with real data how we can actually use this to kind of indicate certain levels by
state but here that's what the color bars are relating to.
And we can see that the title of the color bar goes here just is a title for this color bar.
And you can choose the color scale with whatever string you want that's mentioned in that Jupiter note
book again for this type of plotting.
You'll really have to reference either the documentation of the Jupiter notebook because the formatting
is really kind of bizarre here but that's the way plotless works.
Then you have a layout variable and a nested dictionary object where you just specify G.O. and the scope
to be USA.
It's going to be a USA states chloroplast.
And then you pass the whole thing to go that figure and you say I plot Coro map and you see this here
.
Now go ahead and show an example with some real data as well some other options we can add to the dictionaries
and data and layout.
So go ahead and build on the knowledge you already have going to go ahead and scroll down here and I'm
going to say DFA is equal to PD that read C S V and there's a C S V file here.
Call 2011 us underscore agri underscore exports and these are just agricultural exports by state.
And let me go out and ellipse import Pandurs in order to read that in.
And then we'll check out the head of the state A-frame.
So in the state of frame I have a code of the actual state name category in this case for all states
.
Total exports and then things such as beef pork poultry dairy etc..
So what if these states are exporting for agricultural products.
All right so let's go ahead and get started by creating our data variable.
In our lay out variable my data variable is going to have that dictionary casting and we'll go ahead
and say type is equal to Coro.
And then we pasan a color scale arguments.
In this case I'm going to pass in the string.
Capital Y capital I capital O lowercase our capital are lowercase D.
In this kind of a yellow orange red and the next I'm going to say locations I remember locations.
If we scroll back up here was just the list of state codes and our locations mode is also here.
Now we actually have in our data frame if we look at this a code column.
So I'm just going to pass A.F. code
and the next is going to be our location mode and we'll set that equal to USA states
and we also want to specify what we're going to say for z and z is going to be what the color bar is
actually representing this case.
Let's go ahead and say the total exports.
So we have total exports as a column there and that's going to be our value.
We also have texts and that's just going to be text that lines up.
And if you scroll all the way to the right it's kind of conveniently placed text column here.
Usually you probably have to build your own text column but since this is just an example we already
have a text for you.
We'll go ahead and passen text there
and then we also want to passen a color bar than to say color bar is equal to.
And this is where you have that dictionary where you just have the title of the color bar.
In this case let's go in and say millions USD because that's what the actual total exports represents
.
That's the number in millions of U.S. dollars.
Now we're going to go ahead and add one more argument to this and it's going to be the marker equals
.
And this is kind of a weird argument because it's a set of nested dictionaries but this basically is
going to define a marker that we're going to show once we show the plot and you're going to say cast
a dictionary line is a key equals and then another dictionary casting call and you say color is equal
to an plotless can accept r.g B color as a weird string format.
You say R.G. B.
And then in parentheses you pass in your R.G. B values.
So let's go ahead and just say two 5:55 for all three of these for red green and blue.
And then I'm also going to specify inside of this inside inner dictionary that the width is equal to
two.
And we'll see later on how that marker actually comes into effect.
And then finally I want to make sure that we have a comma here.
So we have the next key and we close it off.
All right.
That's my data dictionary.
Now we're going to have our lay out dictionary.
I'm going to say lay out is equal to how my dictionary casting call I can set the title of the actual
plot.
Now let's go and say this is 2011 U.S. agriculture exports by states that I'm going to say Geo is equal
to and I going to pass in a dictionary here.
We have scope is equal to USA.
I'm going to pass in two additional arguments inside of this dictionary show Lakes is equal to true
and that basically just says show the actual lakes and the map and then you can choose Lake color and
I'm going to passen and R.G. be code using platies notation for our G-B code which is just a string
R.G. B.
And then in-print sees the colors are the values you want.
So we're going to say 85 173 to 40 and I grabbed these from the Internet because they basically make
a nice blue for the lakes and that's going to be our lay out dictionary so we just take a look that
we can have this weird nested dictionary structure but that's the way plotless takes it.
Then finally we can say Koro map to is equal to go figure.
Data equals and we passen that data dictionary into a list and then we just say layout equals lay out
.
And then finally we call I plot's on coral map too.
And here we have now 2011 U.S. agricultural exports by state and notice as we hover over we get to see
all that text those values California.
We also see that the lakes are blue meaning that show lakes and the color of the lakes worked out correctly
.
We have a color scale.
We have our tidal Millings USD and we also see that there's kind of spacing between the states themselves
.
That is what that marker represented.
So if we go back here under data we had this marker.
So we set a line for color and then with so if we increase this with the line by let's say double and
let's go in and change the color to be something like 12 12 12.
So it's really obvious.
I'll run this again and then notice how this map is going to change as far as these borders are right
now.
Very thick black borders site changed those are G-B values and you can do that just by following this
kind of formula here saying dictionary line and then dictionary again you choose the color and the width
of the dictionary.
Well go ahead and just set this back to something like one.
And if I run this again we can see our plot.
All right.
That's how you can create Curle plus plots on a U.S. national scale using plot line.
Now I shouldn't know.
It is pretty confusing as far as the syntax and trying to remember what order everything goes into.
My best advice to give you is to reference a Jupiter notebook and to reference the documentation links
in the Jupiter notebook.
More often than not you'll just have to essentially copy and paste and then fill in the relevant code
for your particular data set.
It's not easy but that's the way you have to do it.
And this is just by nature geographical plotting is pretty tricky because it can come in so many formats
that they try to make it vague so you can pass in as many possible locations or location modes.
But that just leads to kind of weird syntax structure.
That's the nature of this game of geographical plotting as far as trying to use plot Lee as a Python
library for geographical plotting.
But you can see after all that hard work you do get something that is quite nice to show other people
.
All right.
Coming up in the next lecture will show how to do this on a global scale with global coral plus plots
.
Thanks everyone and I'll see you at the next lecture.