Exploring Choropleth Maps
Explore how to create choropleth maps using Plotly Dash by mapping data to geographic locations. Learn to add interactive features like dropdown selectors and animation frames for dynamic year-based views. Understand how to customize map layouts and enhance dashboards with contextual Markdown, enabling users to interpret multiple indicators visually and interactively.
Choropleth maps
Choropleth maps are basically colored polygons representing a certain area on a map. Plotly ships with country maps included (as well as US states), and so it is very easy to plot maps if we have information about countries. We already have such information in our dataset. We have country names, as well as country codes, in every row. We also have the year, some metadata about the countries (region, income group, and so on), and all the indicator data. In other words, every data point is connected to a geographical location. So, let’s start by choosing a year and an indicator and see how the values of our chosen indicator vary across countries.
-
Lines 2–4: We open the
povertyfile into a DataFrame and create theyearandindicatorvariables. -
Line 6: We create a subset of
povertywith values from the selected year and containing countries only. -
Line 8: We create a choropleth map using the
choroplethfunction from Plotly Express, by choosing the column that identifies the countries and the column that will be used for the colors.
We can see the result of the preceding code in the Jupyter Notebook set up below:
The country codes we provided were already included in Plotly and are in the three-letter ISO format. As with scatterplots, we can see that since we provided a numeric column for the color, a continuous color scale was chosen. Otherwise, we would have gotten a discrete color sequence. For example, setting color='Income Group' would produce the chart in the Jupyter Notebook set up below:
Using normal country names
We can also use normal country names to plot them. To do that, we only need to set locationmode='country names', and ...