Learn how to create choropleth maps in Plotly.
- [Narrator] The Plotly library provides the simplest way to make geographic maps using Python. Not only is it simple to make maps with Plotly, but you get maps that are interactive, web friendly, and easily shareable. There are two object types in Plotly mapping. Those are data object and layout object. The data object is a list object that contains a dictionary specifying each of the parameters for the map's data. The layout object is where you specify the parameters for the map's layout.
I'm going to show you how to create a choropleth map and a point map. This is a choropleth map. A choropleth map is a geographical map where spatial areas are colored in hues that represent the quantity of a given attribute in that specific geographic region. There are some specific requirements for generating choropleth maps in Plotly. One is that you need to use a code parameter to select geographic areas by name, for example US States. You also need to pass in the data parameter type equal to choropleth.
Moving on to point maps, point maps are geographical maps with dots placed at specific spatial locations that represent data points. You can add color, size, and shape specifications to point maps in order to add an extra layer of information to your point map. Specifications for generating point maps in Plotly are that you have to provide precise X Y position information about where you want each point plotted, and also you pass in a marker parameter instead of a Z parameter like you do with a choropleth map.
Lastly, you want to pass in the argument type equal to scattergeo in order to generate a point map. Don't worry too much about remembering all of this, because I'm going to show you how to use it in the demonstration. Let me show you how to create maps in Plotly. For this demonstration, we're going to need NumPy and Pandas. We'll import those. And then we're also going to need our standard Plotly tools, so we'll import that. Once we have our libraries, we also need to set our credentials using the set credentials file function, and then for this demonstration, I'm going to use a data set called States.
It comes with the download for the course, and we'll read that in using the read_csv function. We'll print the head here. Let's look at this real quick. The dollars column is a column that describes how many thousands of dollars a state spends on education per year per student. The SATV column describes the average SAT verbal score, and the SATM, the average SAT math score of high school students. You can see that's there no latitude or longitude values here, which may be alarming since we're making a map, but actually we don't need precise latitude and longitude values unless we're making a point map.
This is where the column called code comes in. With the state name abbreviations, we should be able to generate a choropleth map at least. Now let's attach a new column onto the states data frame and call it text. We'll use that column to print out labeled statistics about states as we mouse over. We want to print out information about the SATV score, and SATM score by states upon mouse over. So write the name of our data frame, which is states, and then we're going to call this new column text.
Now we want a label that indicates that we're going to have the SATV score and then directly behind that we want to retrieve the SATV score from the data frame. So we have to write states, and then select that column. We want this to be as type string, because we want a string to show as a label, which makes sense. Next, we'll show the SATM score, and for that we'll select the SATM variable from the states data frame, and say as type string, again.
Let's also add a break. This breaks up the lines. So this is putting the next section of the code on a new line. And then on this new line, we just want to display the abbreviation for the state, and indicate that that's what we're showing. So we'll label that state, and we'll say states, the name of our data frame, and select the code variable. And let's move this down to the next line. Now let's create a data object. It's going to be a list that contains a dictionary.
We'll call it data, and it's a list, so we have a square bracket, and we'll pass in a dictionary constructor. Now we pass in a parameter setting for the data in our map. Let's start by passing in type equal to choropleth. This designates what type of map we want Plotly to generate. So we'll say type equal to choropleth, and then we'll say that auto color scale argument is false. We do that so we can define our own color scale.
So we'll say auto color scale equal to false. Then for the locations argument, you want to specify states code column, because that's the column with the state codes. To do that, we'll say locations equal to states and then the code variable within the states data frame. For the Z argument, we'll set that equal to the dollars column. This will be to value that is represented by the color bar in the map.
Next, what we're going to do is we're going to say the location mode is USA States. This tells Plotly to generate a USA map, and not a map of some other geographic location. That's location mode equal to USA-states. And lastly, we're going to pass in the text argument to define the text that is displayed when you hover over your map. For text, we want Plotly to display the descriptive text we've generated within the text column of the data frame.
So, we say text equal to states and select our text column. To color our choropleth, let's pass the color scale argument, and set that equal to custom color scale. That's a custom color scale that was built for color in Plotly Heat Maps. Custom-color scale. Last, let's pass in the color bar argument, which is also a dictionary, and we're going to pass in the title argument to define a title for our color bar.
Let's make that thousand dollars. So we say color bar equal to, and we call the dictionary constructor when we pass in title, and set that equal to thousand dollars. Now you see if we print out the data object we have a list that contains a dictionary that specifies all necessary parameters for our map data. Let's print that out, and here we have it. This is all the data that we're going to plot out in our map.
One thing about making maps in Plotly is you have to be really careful with your syntax. So, if you have any problems, just check that there are no spare spaces in your coding. Next we need to create the layout object, which will be a nested dictionary. Again here, we'll call a dictionary constructor, and pass in values for the layout parameters. So we're going to call this layout, and it's going to be a dictionary. We'll start by passing in a title argument for the map, and we'll call it state spending on public education in thousand dollars per student.
So, we'll say title equal to state spending on public education in thousand dollars per student. Next we pass in the geo parameter, and we set that equal to a dictionary object that contains the scope of the map, which is going to be USA.
We also want to define the projection as albers USA. So we'll say projection, and then this is also going to be a dictionary object, and we'll say type equal to albers USA. I'm going to move this down so we can see it. Let's also pass in a parameter for show lakes, and we'll say show lakes equal to true. We want to see the lakes on our map right? And set the lake color equal to a standard blue color. So for that we'll say lake color equal to and then pass in an RGB code and for this we're going to use 66165245, and then we'll print this out just to see what our layout object looks like.
These are our layout parameters for the map. What you can see here is that it's a nested dictionary that contains the parameters for our map layout. That's perfect. The next thing we need to do is create a new dictionary object that contains both the data and the layout objects. We'll call it fig, call the dictionary constructor, and say data equal to data, and layout equal to layout. To plot this we just call the I plot method on our fig object, so we say PY.iplot, pass in our fig object, and then give it a file name, which will be D3.choroplethmap, and print it out.
And we have a quick choropleth map. That's pretty awesome, right? It's interactive. You can zoom in, zoom out, move it around, and what is it saying? It's saying basically that according to this data, the states in the northeast are spending the most money on their public education, and as well as Alaska. We just finished the choropleth maps, and now I want to show you how to make point maps. Making point maps in Plotly is very similar to how you make a choropleth, but some of the parameters are different.
I'm going to show you the changes you need to make in this next demonstration.
- Getting started with Jupyter Notebooks
- Visualizing data: basic charts, time series, and statistical plots
- Preparing for analysis: treating missing values and data transformation
- Data analysis basics: arithmetic, summary statistics, and correlation analysis
- Outlier analysis: univariate, multivariate, and linear projection methods
- Introduction to machine learning
- Basic machine learning methods: linear and logistic regression, Naïve Bayes
- Reducing dataset dimensionality with PCA
- Clustering and classification: k-means, hierarchical, and k-NN
- Simulating a social network with NetworkX
- Creating Plot.ly charts
- Scraping the web with Beautiful Soup