From the course: Data Visualizations with Plotly

Mapping with Plotly - Plotly Tutorial

From the course: Data Visualizations with Plotly

Start my 1-month free trial

Mapping with Plotly

- [Narrator] Another exciting type of visualization that Plotly has great support for is maps. Maps are so integral into our daily lives that they make for highly intuitive and meaningful data visualizations. So let's see what Plotly has to offer. After importing Plotly Express as PX, we'll import the gapminder dataset for the year 2007. Next let's construct our figure for a scattered geo plot. This will give us a simple scatter plot on a map. For locations, gapminder has an ISO alpha field which Plotly can map very cleanly. Using location mode, scatter geo can interpret ISO codes, country names, or US state names. We'll color our scatter plot by continent, and we'll size by country population. The result is a clean map with markers for each country sized by that country's population. Another modification we can make is to change the map projection. We'll demonstrate with the natural earth projection. This projection looks great. The second map type to understand is called a choropleth, which is a field area map. This type of visual is very easy to understand, but it does have its downsides. Namely, it can give a false impression of a breadth changes occurring between boundaries. Think of an electoral map, for example. Using PX.choropleth, let's examine life expectancy by country in the gapminder data. This visual does a great job representing the disparities between life expectancies across the globe. Next we'll explore a more advanced application of maps with Plotly. To do this, we'll use a geo data equivalent of pandas called, you guessed it, GeoPandas. This can be installed via PIP. Additionally, we're going to use the Mapbox API to enhance our map with satellite imagery. While not necessary for this example, you can also sign up for a free Mapbox account, customize your map layers. We're going to read a CSV of Colorado's 14,000 foot peaks into a Panda's data frame. Then using GeoPandas, we'll convert the data frame to a geo data frame and assign what is called a geometry to the longitude and latitude readings for these peaks. In our new geo data frame, we see how geometry represents our coordinates through point. Now we'll use scatter Mapbox, assign lat to geometry.y, lon to geometry.x, and we'll color by the range the peak belongs to. Great, now we've created our figure object. We'll use update layout to add a title and use a USGS satellite imagery layer from that box. The resulting visual is pretty cool and you can see it's especially useful to have satellite imagery on this visual. Map visualizations with Plotly are among the most fun to work with, and they really resonate with end users. So even if you don't have geographic data readily available, I recommend you search for geographic datasets online that are interesting to you. Then give these techniques a try.

Contents