plotly Map in Python (3 Examples)
Hi! This tutorial will show you how to make interactive plotly map in the Python programming language.
We will look at examples of how to make a choropleth map in Python.
Here is an overview:
Let’s get into the Python code!
Install & Import plotly Library
In order to build an interactive map in plotly, we will need to install and import plotly in our Python programming environment. So, in your preferred Python coding IDE, run the lines of code below to install and import plotly:
# install plotly pip install plotly # import plotly express import plotly.express as px
With plotly installed and imported into our Python environment, we can now access its map-building functions, including the ones we will make use of in this tutorial.
Example 1: Build Simple Choropleth Map
In this example, we will build a simple choropleth map using the election dataset and GeoJSON file that come pre-loaded in plotly.
To load and preview the election dataset, run the lines of code below:
# load GeoJSON file geojson = px.data.election_geojson() # load dataset df = px.data.election() # view first 10 rows of the dataset print(df.head(10)) # district Coderre Bergeron Joly total winner result district_id # 0 101-Bois-de-Liesse 2481 1829 3024 7334 Joly plurality 101 # 1 102-Cap-Saint-Jacques 2525 1163 2675 6363 Joly plurality 102 # 2 11-Sault-au-Récollet 3348 2770 2532 8650 Coderre plurality 11 # 3 111-Mile-End 1734 4782 2514 9030 Bergeron majority 111 # 4 112-DeLorimier 1770 5933 3044 10747 Bergeron majority 112 # 5 113-Jeanne-Mance 1455 3599 2316 7370 Bergeron plurality 113 # 6 12-Saint-Sulpice 3252 2521 2543 8316 Coderre plurality 12 # 7 121-La Pointe-aux-Prairies 5456 1760 3330 10546 Coderre majority 121 # 8 122-Pointe-aux-Trembles 4734 1879 2852 9465 Coderre majority 122 # 9 123-Rivière-des-Prairies 5737 958 1656 8351 Coderre majority 123
With our dataset and GeoJSON file loaded, let us now build a choropleth map:
fig = px.choropleth(data_frame = df, geojson=geojson, color="Joly", locations="district", featureidkey="properties.district", projection="mercator") fig.update_geos(fitbounds="locations", visible=False) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show()
In the px.choropleth() function, we parsed the dataset to the data_frame =
argument, which takes either a DataFrame or an array-like object.
We then set the color to “Joly”, and location to “district”, both of which are columns in the dataset. The featureidkey =
argument takes a path to a field in the GeoJSON feature object with which to match the values passed in to locations =
argument.
As for the projection =
argument, there are so many options of the kind of projection you can parse to that argument. You can see the options here.
The update_geo() function takes the argument fitbounds =
, which determines if the plot’s view settings are auto-computed to fit trace data. If set to “locations”, then only the trace’s visible locations are considered in the fitbounds computations. Since we don’t want the fitbounds to be visible, we set the visible =
argument to “False”.
Example 2: Build Simple Choropleth Map with Discrete Colors
In this second example, we will build the same map as in the previous example, but will instead use discrete colors to color the map:
fig = px.choropleth(data_frame = df, geojson=geojson, color="winner", locations="district", featureidkey="properties.district", projection="mercator",hover_data = ["Bergeron","Coderre","Joly"]) fig.update_geos(fitbounds="locations", visible=False) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show()
The code remains practically the same as in the previous example, with the only difference being that, here, we parsed a discrete variable to the color =
argument in the px.choropleth()
function, which colored the map by the categories in the “winner” variable.
We also parsed an array to the hover_data =
argument, which displayed the data on hover.
Example 3: Build Simple Choropleth Map by Defining Discrete Colors
In this last example, we will build the same map, but will define the discrete colors to color the map:
fig = px.choropleth(data_frame = df, geojson=geojson, color="winner", locations="district", featureidkey="properties.district", color_discrete_map={"Bergeron":"#00ffff", "Coderre":"#cc00ff", "Joly":"#ffff00"}, projection="mercator",hover_data = ["Bergeron","Coderre","Joly"]) fig.update_geos(fitbounds="locations", visible=False) fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0}) fig.show()
The code is virtually the same as in the previous example. The only difference, this time, is that we parsed a dictionary to the color_discrete_map =
argument in the px.choropleth()
function, which assigned colors to each of the categories in the “winner” column of the dataset and colored the map accordingly.
Video, Further Resources & Summary
Do you need more explanations on how to build interactive plotly maps in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to build interactive plotly maps in Python.
The YouTube video will be added soon.
With that, we have demonstrated how to build an interactive plotly map in the Python programming language. As you can see, it is easy to build a choropleth map and visualize geospatial data in plotly.
- plotly Treemap in Python (3 Examples)
- Change Size of plotly Graph in Python (Example)
- plotly Area Chart in Python (5 Examples)
- plotly Heatmap in Python (3 Examples)
- Custom Button in plotly Graph in Python (Example)
- Learn Python Programming
This post has shown how to build interactive plotly maps in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter