Geographical Plotting with Basemap
00:01 Speaker 1: Hello, everybody, and welcome to the second part of Section V: All About Data Visualization with Matplotlib in Python. In this section, what we're gonna be talking about is Basemap and actually, getting Basemap. To get Basemap, the first thing that we're gonna wanna do is go to matplotlib.org/basemap.
00:20 S1: Now, once we're there, what we wanna do is we wanna download Basemap, 'cause you won't have Basemap by default. So, we're gonna wanna go to "Download here" and it should take us to this website here, SourceForge. And from here, we can find the versions of Basemap, and then what we actually want. I've already begun my download but what you would do is you could come down to Basemap 1.0.7. Go ahead and click on that. And then from there, we see there are some Windows Installers but the problem is we don't have an installer for Python 3.4. So, we're using 3.4.
00:56 S1: So, what we're gonna wanna do is use this right here. We'll just get this "tar.gz". Go ahead and click on that and download that. I'm gonna go ahead and pause it while mine is downloading and then once it's done, we'll go ahead and pick back-up. In order to extract this file, you're gonna need a program called, 7-Zip. That's seven-dash-zip, which you can find here. Now, if you're on possibly Mac, you might be able to do it and then obviously on Linux, you can unzip tarballs, no problem. But if you're on Windows, you'll wanna program like 7-Zip. So, 7-Zip's the only one I know of that can even do it on Windows. But if you're on Mac or Linux, you can also do it if you'd know how to unzip tarballs and stuff. Otherwise, just check out 7-zip. Anyways, I'll pause it now and then whenever the download is complete, we'll pick it back up.
01:50 S1: Okay. Once it's done downloading, what you'll do is you'll go into wherever it was downloaded to. Here's where mine is. You can right-click it and we'll extract it with 7-Zip. We'll just say, "Extract here." And again, if you're on Mac OS or Linux, you would use the "tar -zxvf", and then the file to extract. Once you've extracted, you should have this "dist" folder for the distribution and there, you've got another tar. So again, we'll do the exact same thing with 7-zip and we'll extract here. That's fine. And then, clicking in there, we see we have all these files along with a, "setup.py."
02:30 S1: Now, the easiest thing to do from here is to use this "setup.py" file. Let's go ahead and go back one directory and at least on Windows, we can hold Shift and right-click that directory and open a command window there. Now, we're basically within this Basemap directory and then, we can say specifically, "C: /python34/pythonsetup.pyinstall". This will attempt to install Basemap into our Matplotlib.
03:04 S1: What we would do at this point is we're given an area at least here and we can do "C: /python34/python" and that's gonna basically run Python for us. And then, our question would be something like this: From "mpl_toolkits.BasemapimportBasemap". As long as you can do that, you should have Basemap pretty much all set up. As long as you can import Basemap like we've just done there, we're good to go.
03:45 S1: If, for whatever reason, that doesn't work out for you, you can go ahead and get the "Wheel" download and the way that you can do that is by coming to this unofficial binaries source here. Here's the link there that you would use and from there, we can get Basemap. You can just do "Ctrl-F, Basemap" and here we are. For me, I'm on a 64-bit version of Python but don't be confused. If you're on a 64-bit operating system, you still probably have a 32-bit version of Python. So, only if you purposely tried to get a 64-bit version of Python, do you have one. The default is 32, regardless of your operating system. Figure out which one you are, and then you could download. Because the newer versions of Python come with pip and this version of pip understands Wheel, what we can do is... Let's go ahead and let me get out of this real quick. Let's do "Quit". Okay. Let me change directory. Actually, we can't do that. Let's just close out of this.
04:47 S1: Let's say, you were gonna redo it. I'll open the command window here I'm in the Downloads and what we would do now is we would do "C: /python34/scripts/pipinstall", and then, we would just do Basemap, hopefully. Let's try one more time. There we go. We would install the Basemap 1.0, and this one's actually a newer version. This is 1.0.8 which I didn't even see on the SourceForge. Anyway, you could do that and this may actually update me. We'll see what happens. We can go ahead and try that and we'll see if that works for me. It says it did.
05:29 S1: We could check it again by "C: /python34/python". Now, we're running Python from "mpl_toolkits.BasemapimportBasemap". And again, as long as that works, you've go the correct installation. Now again, the Wheel file, you'll need to use pip to install or you can download the source although that was interesting that I did not see the updated version. That's interesting. Yeah, by going to SourceForge, I don't know where you would get the updated version. [chuckle] But apparently, we got it on the unofficial source. So, anyways, there you go. So, that's it, hopefully, you have something running now. In the next tutorial, what we're gonna actually do is bring up our first representation or graph or chart or whatever of a map basically. So, stay tuned for that. Thanks for watching.
00:00 Speaker 1: What is going on, everybody? Welcome to part three of section five of our data visualization with Python using Matplotlib tutorial series. In this what we're talking is the Basemap kind of addition to Matplotlib, which allows us to visualize geographic, and even topological types of maps with Matplotlib.
00:25 S1: So to start, what we're gonna go ahead and do is... First, we need to bring in Basemap, so what we do here is from "mpl_toolkits.basemap import Basemap". So this is part of MPL toolkit, so when we installed it, it's not... To use it, we don't do the typical import Basemap, it becomes a part of Matplotlib. So moving along, we need that to utilize Basemap, and then we also need to bring in pyplot again, because just like pyplot 's integral to every other Matplotlib graph that we've covered so far, it's gonna be integral here. And we're gonna use it to show, and also add title, and stuff like that. So, we're going to import "matplotlib.pyplot " as PLT as usual.
01:22 S1: Now, with computers, they don't tend to understand north, south, east, and west, right? So they don't quite understand latitude and longitude right out of the box. So that said, just like time and date stamps, we can handle, for latitudinal and longitudinal coordinates, we just have to make some sort of conversion. So what ends up happening instead is that western numbers... 'Cause latitude is up and down, longitude is left to right. So, western numbers would be part of your... Let's say that would be, longitudinal, would be west and east. Western numbers become negatives, and then eastern numbers would be positive. And then, northern numbers for, let's say, latitude, these would be your positive numbers, and southern numbers would be your negatives. For example, what would the latitude and longitude for, say, New York City be, where New York City's coordinates are 40.7 north and 74 west? Well, that would leave you with a 40.7, -74. Those would be your coordinates. So with that, let's go ahead and plot our first graph.
02:47 S1: First, we say... You don't really have to necessarily do what I'm about to do, but you pretty much do. [chuckle] So, we'll say "m = Basemap" and then we won't really put anything in there just yet. And what that does is that it basically initializes Basemap, and it's a lot like when we did 'ax1 equals' and all that other stuff. What this allows us to do is use "m" as our map; m is just short for map. And it allows us to take that and then modify it later on. Now, we can leave it like this. Basemap comes with defaults, so it's a lot like pyplot, where you don't actually even have to populate it with anything. So we'll say, "m = Basemap", but then we have to at least do something simple and maybe draw... Let's draw coastlines. So we can just say, "m.drawcoastlines" and that will draw lines for the coast. And then that's basically it. We call a "plt.show" and we can show this graph now. So, let's save and run that, and up pops just a really simple graph. And that's it, it's just everything is a default.
04:00 S1: We're drawing coastlines, but we can go further with this and we can do "m.fillcontinents". So this is just coastlines, and then what we can do is, now we can fill for the continents within coasts. And so, what's happened is the water areas are left white, whereas continents, and basically land, is filled in as a gray. And then, another thing that we can do is we could say "m.drawmapboundary". And at least right now, it doesn't really make too much of a change, but what it will do for you, is it will draw, basically, boundaries on a map. So, right now, it's not really doing stuff 'cause we haven't really added any boundaries for it specifically to do for us, but just keep that it mind that's another example of what we can apply.
05:00 S1: Now, going into the Basemap here, this is where we can start to customize the actual representation because the world is cylindrical, so if you took a flat map and attempted to... Let's say you got a map from your car and you've got a big exercise ball, and you try to wrap that map around that ball, you're gonna have a really hard time. [chuckle] And the reason why is because the map is usually square and the ball is spherical, but the world is also spherical, so any square representation of a map is just, de facto, distorted. So, if you tried to wrap that map around the ball, you could do it somewhat, but things would not work out in your favor. We have to understand that with maps, there's all kinds of representations.
05:53 S1: So, depending on what your goals are, right? If you're a navigator, and you're navigating between North America and South America, or maybe you're a navigator and you're purely navigating, maybe, between states or countries in Europe, you're not probably too worried about the distance between Africa and North America, or you're not really too worried about the distortion of size of Canada as compared to maybe Antarctica or something like that. But you are... Let's say if you're traveling in a ship across the ocean from Africa to the United States, you would want the distance to be relatively representative of the distance you're actually going to travel, okay. So, that would be an example of when, maybe you would want that area to not be distorted, but if you're traveling just from North America to Africa, you're probably not so worried about, maybe, Russia being a little distorted in size or Antarctica to be distorted, or Greenland to being a little distorted, or something like that.
06:58 S1: So, anyways, there's a lot pf projections for a lot of reasons and a lot of uses. So, it just depends on what you're doing but I'll show you how we can find a bunch of the projections. But for now, let's just use a simple Miller cylindrical projection. So, in Basemap, we can say "m = Basemap", and then, we first can say the projection, and we can say that equals basically, whatever we want, but we'll say, "Mil," which is short for Miller. And then, we have the coordinates that we can... So, for example, we can have "llcrnrlat". Now, what this stands for is Lower Left Corner Latitude and then, we have Lower Left Corner Longitude and then, we have the Upper Right Corner Lat and Upper Right Corner Longitude. So, what ends up happening is, let's say you're... We're using Paint here, and we've got our coordinates. There we go. And you've got your coordinates here, and these are your longitudinal coordinates, and these are your latitude coordinates.
08:13 S1: And so, let's say you're... This is, maybe, 10 here. Okay, let's just say 10. This would be negative 10, and this will be our negative 10 mark, and let's say you want something to be a box, you want your map to be this box here. Well, the way that you would do that is you specify what this coordinate is, and you specify what this coordinate is. And then, what Basemap is gonna do, is it'll fill that in for you; it'll say, "Okay, well, that's fine." We'll take this coordinate, and then, all we do is we draw a line up into the latitude of the upper right corner's latitude. And then, we, from there, draw a line to that longitudinal coordinate. And then, again, from here, we draw a line from this coordinate here down to the latitude value of this coordinate here. And then, we do the exact same thing again with the longitude going back, and that's how it draws that square. So, when you wanna draw a square, that's basically how you're gonna do it. Now, we'll go ahead and cut it off here. And in the next tutorial, we'll actually draw our square and we'll talk about how that's done. So, anyways, stay tuned for the next video and thanks for watching.
00:01 Speaker 1: Hello everybody, and welcome to Part 4 of Section 5 of our data visualization with Python and Matplotlib tutorial series. In this session we're talking about Basemap, and what we were doing in the previous tutorial was representing just a really simple example of a map, which was this. And then we started talking about the projection, which currently we're using Miller, which for the most part probably looks the least distorted of any of the charts that you'll see. Except for Antarctica's massive, but other than that we're looking pretty good. So, what we're gonna continue on with in this tutorial is we're gonna be talking about how we can customize this projection. So most of that projection customization happens right here, within this Basemap method. So right now we're defining the projection as "Mill" which is for Miller. Now let me bring this down. So this would be our Miller Projection, but there's a ton of projections and finding them you can come to this Basemap/...
01:01 S1: I get this all the time with matplotlib.org. But usually it'll just take a second, like you can see its loading right now. But I get stuck on this and it crashes for me all the time. I wish it didn't. But anyway, I'll show you how to get there whenever this loads. Let's try to refresh. When you find the projections you will see stuff like this. Like the Azimuthal Equidistant Projection so you could see how that looks. Then you've got the Mercator, and then you've got like Cassini is an example, this is the Millers the one that we're using. And you can read a little bit more about them as far as what they want. So this is a modified version of the Mercator which we just looked at, so you can see this compared to this. There's not much difference there. Now, there we go. So to get there basically you would come here and then you could go to like "Setting up the Map." Basically you can click on that, and you can see all of the possible projections here.
01:56 S1: So that's quite a bit of projections that they give you. And that's just the background of the map. The map doesn't have to look like these maps, it's just the... Basically the distortion of the map, well, will be like these are. So, it just depends on kind of what you're looking for. Now anyway, so there you go, those are your projections. We're gonna continue using the Miller for now which is short... Mill is for short. The next thing like we started talking about before is, you specify then the lower left corner lat/long, and then you specify the upper right corner lat/long, and then Basemap is gonna create a box using those coordinates. So what we're gonna do, is we're gonna do lower left CRNR lat. So lower left corner lat will be equal to -40. Then we'll do lower left corner long, equals... We'll do -40 here too.
03:03 S1: Then we'll do upper right corner lat, equal to 50, and then upper right corner long, equal to 75. So let's save and run that. And what we get is basically Africa. Okay, a little bit surrounding, but basically Africa is in the middle. So it's clearly a graph that's gonna be something about Africa, and maybe some of the closer regions, but mostly Africa. So, closing out of this now, there's another major parameter that we're gonna see a lot and that's gonna be the resolution. Resolution can be a bunch of stuff, by default its gonna be C. And what C stands for is Crude. So it is the most basic resolution that you're gonna see. Now, this is pretty good for a lot of maps that you might need, but you might wanna increase this resolution for all kinds of reasons.
04:01 S1: So that was crude, and then you can increase that to L. And L is just low. And you can see now that we've got maybe some rivers in here, maybe a few of the larger bodies of water, stuff like that. And that's on low. And as you keep going up the resolution, it's gonna take a little bit longer to load each time. And so now we can go to, let's say that was L, so we can go to I now. This will be intermediate. So as you can see it's taking a little bit longer now just to even bring it up but there it is. And now we can see okay, we've got some islands over here and a little bit more rivers and smaller bodies of water showing up and this is our intermediate one. So that's cool, okay we've got that. Then after intermediate there is H for high. And this one's gonna take even longer for us, but there is even one greater one than high. And each time you upgrade basically or each time you downgrade it's basically 80% downgrade.
05:06 S1: So each one is pretty significant decline from the previous. Okay, this one popped up, it just popped up down here. Okay, so here again, we have even more bodies of water and stuff. And what this is useful for if you're zoomed out this far you might not know but, we can still use the same controls and stuff that we had used before, so we can zoom in to this little body of water. And you see that, oh, my gosh, look at all the little intricate details on this big one. And then we can zoom in to one of these little ones here, and then again look at the... There's so much intricacy on this little bodies of water basically that we're able to come in on and actually see. Or we go over here and zoom in again and again we've got a pretty good detail here. I mean eventually we can zoom in and we can see some aliasing going on. But for the most part it's pretty impressive on high. And we can do one more and that would be F for full and that would give us the best resolution that is possible for us on Basemap.
06:09 S1: Now, once we've made it this far, that's some of the main things that you're gonna pass through Basemap but another thing I just wouldn't mind showing although we're already kinda loading here. But the next thing we'll load, we'll just load it low and what we can do is we can say plt.title. So, we can add titles like we've learned it before and we can call this "Basemap example with title", something like that. And we'll be able to see that on the figure, we're able to add a title. Now, I'm still just kinda waiting for this full resolution to come up, just so I can show it to you guys.
06:46 S1: Depending on your computer, it may be quicker or slower than my computer so by default, Python works with your CPU, not your GPU even for graphics. And then, to make it even worse, by default, it only uses one thread at a time so, you're not gonna be utilizing all your cores or anything like that to display stuff. Depending on how fast a single core is on your CPU, that's what's gonna cause this to be either faster or slower. I'm actually pretty surprised how long this is taking. But as you could see before as we're zooming in, the level of detail is actually quite high but one thing you can do is by zooming in to a specific area using the coordinates. You can save a lot of processing there. Also, obviously the resolution is gonna save some and all that.
07:36 S1: So anyway, the chart's up now so, let me pull it up for you guys, and there we go. We can just see that it's just even more levels of detail here. It's a little laggy for me to make it bigger. But we can see these little, tiny dots out here but we can zoom in to those, and then, continue zooming in, and continue zooming in, and we can see just how much detail there is on these full representations. It's really cool because it's not just a simple graph. It can be a simple graph if you need it to be so we'll go back. Yeah. We have a low here. So this is a lot quicker for us to pop up. And you can see now, we've got our title in here and all that.
08:15 S1: So, if you just need a simple graph and even this one shows some of the more major stuff, but we're missing all the little islands that were over here before, but we do actually have a few islands. But anyway so, we can see that we have a lot of options there as far as resolution is concerned and all that. Obviously, even at this level of zoom, you probably wouldn't be using the full resolution. You would only use the full resolution if you were zoomed in to a really specific area. So, yeah, keep that in mind. Anyways, that's it for this kind of customizing the projection section. And, in the next one, what we will be talking about is a little bit more customization options that we can do like coloring and all that. So, stay tuned for that. Thanks for watching.
Log in to save your progress and obtain a certificate in Alison’s free Data Visualization with Python and Matplotlib online course
Sign up to save your progress and obtain a certificate in Alison’s free Data Visualization with Python and Matplotlib online course
Please enter you email address and we will mail you a link to reset your password.