Creating Geographic Maps with plotly express

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone andrew welcome to this section in this section we're going to learn how we can plot geographical data using plotly express and let's just go ahead and begin now the first thing we're going to do is to load up a reference map or reference csv file we're going to be using so we're going to use the gap minder built in data set so i've gone ahead and imported bloody plotly express spx and also pandas i'll go ahead and get rid of the header and also the toolbar so we can have some a little space here work so right now i'm just going to create my country data i'm just going to say country underscore data it's going to be equal to our dot data dot gaap miner just like that and we can actually check out the country data dot tail so we can actually see the last uh few elements within this uh data set so it says country data it's not defined just quickly go ahead and get rid of that uh error like that and there we have it so we have that uh country data information showing so let's go and see how we can do this so the first thing i'm going to use is to use as jio scatter plot so if i do a px dot and i press the tab it should actually show me the uh scatter plot so let's go ahead and check that out so i can just scroll down right here now we have a scattered geo so this is what i'm going to be uh using this scatter geo right so i'm going to be using this one if you want to actually go ahead and see the content for that api we could actually do a simple help on scattered geo and if you run this we should see information on that api and we can actually read up on some of the parameters we can add to that information well let's just go down here and let's create a an object to hold a reference so i'm going to say map wonders config and this is going to be uh equal to our px dot scatter underscore geo so now that i have this function what i need to pass in first is a uh data frame right so we actually have our country data which we created right here so i'm going to use that so i'm just going to say country underscore data and it needs a set of locations as the uh properties so i'm going to use the iso underscore alpha as the locations now the iso is an international standard that is used to specify the locations of a country based on these country codes so if you actually go back to our data frame we can actually see that iso that is a three letter character of zwe and we also have the iso numeric so we check that numeric column and also the iso alpha code this should actually be passed in and we have those are columns on our data frame so we're going to be using that value for the locations so for the locations i'm going to set it to iso dash alpha which means i'm using the alphabetic column so for the projection i'm going to use the autographic and for the color i'll just use the continent for the color so i'm going to say color to equal to continent and for my opacity i'll just set that to uh point eight for the opacity and for the hover name so i'm going to use the country all right just like that so i'm going to be using the country for the hover name and for the hover data so i'm going to say hover on let's call data i need to pass in a list so i'm going to get the life expense expectancy so it's going to be live exp and also the population so i'll use pop so i can have that information also show when i hover the mouse over this uh data so another thing we can uh okay we can actually leave it like that you can go ahead and add extra uh properties if you want but i'll just leave it as this and then finally i'm just going to say map underscore fig and dot show and if everything runs fine whoops so let's see uh it says value hoover name is not the name of a column in the data frame so let's go ahead and see what we've done right here so we've set the overall underscore name over underscore name we'll set this to a country all right so uh it's a typographical error so let me just go ahead and run this again so it says uh the locations is the name of a column that it needs the value of the location so let's go and see what that problem is so we have locations i'll set that to iso it's actually not uh dash it's on the score so i forgot to type the shift for that so we actually ran into that problem so you can actually clearly see sometimes it's the typographical errors and little things that really matter but the most important thing is to look through them carefully and understand where that is coming from so uh yeah so we can actually see we have this information and we can see our hover data is actually showing uh the life expectancy and population if you wanted to add more information we could just check maybe the year and let's go ahead and just do that so for our over data i'm just going to add the year which is actually a column that exists within this uh data frame and if you run that we also have the year so that's clearly how we can add this kind of uh map another thing i'd like to show you is how you could actually plot this information let's say you need this information on your html page so uh it's actually basically straightforward we'll just use the offline method within uh plotly so first let's import plotly i'll just get back up and say import plotly just like that once we've imported plot me i'm going to run this and i'll just get down over here since we've imported plot b so i'm just going to say plotly dot offline dot plot and i want to plot my map on this config which is the object and then we need to pass in a file name argument so i'm just going to paste this or copy this entire document or the html file into my drive c so i'm just going to say c column backward slash m a p i'll just call that map underscore experiment and i'll set the dot html and i'll set auto open to true because i want this to open when i run this so i'm just going to say auto underscore open is going to be equal to true so let's just go ahead and run that and this should open up on another uh the same web browser page i'm using google chrome and you can see we can actually have access to this information on google chrome if i go ahead and jump into the location where i start put it in my drive c and if i get down i'm going to see the map i just created right now it's called map underscore experiment and it's a three megabyte html file so uh i don't want to go into the details of this but if you actually open it up you can see the html code that has generated the this project so let's go ahead and jump back into our work and continue so let's get back to our map data and it's going to show you the output path of where that map data is if you don't want to open this automatically you can just set this to false and it won't actually pop up and open this when you're working with your uh file great and another place we got that information from our so that's nice so let's go ahead and move on so let's actually use the density map box to plot the earthquake uh data so if i go back here to uh github our raw github file we can actually see the uh data when we actually go so let me just go ahead and take a step back to get back to the github data set the github data sets and i'm just selecting the earthquake data set just right here uh it's under data sets earthquakes dash 23k it's a very large uh data set that has the earthquake data so make sure you're using the raw file and remember how we got access to the raw file so if you check down here and find the earthquake data make sure you click on the raw right here where it says raw make sure you click on raw and when raw opens up let's go ahead and just copy the path to the raw earthquake data file and get back to our document and let's go ahead and create that earthquake file so right here i'm just going to create a string or object called earth earthquakes and i will just pass in this entire uh string and i'll make sure to convert this to a string by putting our codes and what i'm going to do is to break this line into two and then use a backslash just to show that this is a string right so what i'm going to do next is to create a data frame so i'm just going to say uh df it's going to be equal to pd.read underscore csv to read the csv file and then we'll pass in our earthquakes like that and if we want to actually see the uh first few elements let's just uh run this and we can see our first five elements within that earthquake uh data frame so what i'll quickly do next is to create a figure and then use store that value in a density map box so uh let's go ahead and just say fig equals to our plotly express dot density on the score map box all right so it needs a data frame as an argument and also needs a longitude and latitude so how do i know what it needs so let me just quickly go up here and create a cell above so now i have this cell above i'm just going to type in help on the px dot density under star box just like that and if i run this i should actually see the list of our arguments that can be passed into this so it needs a longitude and a latitude and also we can pass a z data with a hover name just like we have in our density plot so let me just go ahead and close this and move on to pass that information so for my latitude i'm going to use the latitude column so this is a la latitude for the longitude i'm going to use the longitude column i'm being very careful to check my spelling so i don't know so i'm using the longitude and latitude columns and let's just go ahead and keep on adding some of the parameters we need so for my z i'm going to set this to the magnitude magnitude for the radius i'm going to set that value to 10 and i want to center this so i'm just going to say center and this is going to be a dict dictionary object that is going to take two values of the longitude and latitude so i'm just going to say that equals 9 along let's just use a nine by nine or nine by eight anyone it doesn't matter so and then i'll set the zoom factor to one so for the hover name i'll just set this to the date and then for the map box style map box i think it's underscore style i'm going to use the uh watercolor the stamen watercolor so stamen dash watercolor so how do i know that there is a map box style called stamen watercolor again it's from right here if we actually jump down here and let's just keep going under the macbook style we have basic that's going to be provided some of these tiles need an api key but we don't have that so this is where i got it from and we can have the uh these values that do not require a api token so we have the osm which is the map we have white background cartoon positron carto dark matter stamen terrain stamen toner stamen watercolor so i'm using the stamin watercolor but you can feel free to use any of the free you know map box style options that are available and for the title i'm going to set the title to let's say earthquake data 1965 oops 1965 to 2016 and then i'll just do a fig dot show so we can actually show that figure so again let's go carefully look so we have our data frame which is uh the gapminder data free earthquake data frame we've created right here so we're good to go and then we have our latitude we have our longitude we also have our magnitude for that earthquake we have the radius we have our center position we have to set to uh latitude nine longitude we've set a resume factor of 1 or over name with the date option we have the watercolor map box if everything seems good let's just go ahead and run this and we have this uh information you know showing so if we actually take our cursor over these points we can actually see these dates and we can actually see the uh magnitudes for those earthquakes in those locations based on the longitude and latitude data so let's go ahead and change this to let's say your open street map so i think it's a open dash street dash map right so we can actually change that let's see if we've got that spinning right sorry on the map box tile yeah i think we're right it's it's uh let's just go ahead and test it okay we're not getting any output so i'll just go ahead and check out and see why so it's actually not and i think why it's not showing is simply because of i forgot this t been making a lot of mistakes right here it's not nice so yeah so we can actually see the open street map option showing us those locations and we can actually play around with them and you know kind of customize this to our liking and yeah so basically that's how we can uh kind of like use this uh data set and when we zoom in we can actually see more of the information popping up and we can know which uh which of the region where that uh information is coming from for instance we can see this is like on a border between kenya and tanzania so uh thank you very much for watching guys really sorry about the typographical errors i've been making a lot of typographic errors and that's not really nice but it's i actually like leaving them so you can see where those arrows are coming from sometimes it's not a module problem we just actually make mistakes really sorry about that guys apologies in our next lesson we'll be looking at the uh coral play maps coral coral pleth and we'll see how we can use the choropleth map to plot world data thank you very much for watching guys and i'll see in the next lesson
Info
Channel: 101touchapps
Views: 4,505
Rating: undefined out of 5
Keywords: plotly, choropleth, maps, python, plotly express
Id: Oht6cf-Acl0
Channel Id: undefined
Length: 18min 17sec (1097 seconds)
Published: Mon Nov 02 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.