Skip to content

Commit b4403ee

Browse files
authored
Update README.md
1 parent e5fcc52 commit b4403ee

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,59 @@
11
# Choropleth-Maps-in-Python
22
The easiest way to build a choropleth map in Python using Plotly
3+
4+
After the release of plotly's latest version (version 4.1.0), building a choropleth map in Python is no longer difficult and complex.
5+
All you need now to build a choropleth map are only three things:
6+
- The latest version of plotly which comes with a new chart type -- **Choroplethmapbox**
7+
- A geojson file which contains the coordinates of the country/regions that you'd like to draw
8+
- A csv file which contains the data that you'd like to plot on the map
9+
10+
## Install plotly
11+
plotly.py can be installed using pip or pip3
12+
13+
```
14+
$ pip install plotly==4.1.0
15+
```
16+
17+
or conda.
18+
19+
```
20+
$ conda install -c plotly plotly=4.1.0
21+
```
22+
23+
## Find your geojson file
24+
You can find the maps for most countries in the world in topojson format via this [repository](https://github.com/deldersveld/topojson).
25+
After finding the right topojson file, you will need to convert the file into geojson format. While you can also write python codes to convert the file, I find this [website](https://mapshaper.org/) rather easy to use.
26+
27+
Note that the geojson file is a dictionary which typically contains two keys:
28+
- **type**
29+
- **features**: a list containing multiple dictionaries, each of which has four keys - *type*, *geometry*, *properties*, *id*
30+
31+
In the geojason file of Chinese map I used this time, each dictionary in features represents a province in China, where the geometry contains the coordinates of the province, the properties contains the province's specific info, and I used the province's name as its id.
32+
33+
***id is an important feature that will be used to plot your data on the map later**
34+
35+
## Basic codes for mapping
36+
37+
The basic codes for mapping are as follows:
38+
```
39+
fig = go.Figure(go.Choroplethmapbox(geojson=china,locations=geo.Regions,z=geo.followerPercentage,
40+
colorscale='Cividis',zmin=0,zmax=17,
41+
marker_opacity=0.5, marker_line_width=0))
42+
fig.update_layout(mapbox_style="carto-positron",
43+
mapbox_zoom=3, mapbox_center = {"lat": 35.8617, "lon": 104.1954})
44+
```
45+
46+
I will explain some of the most important features in the codes above.
47+
- geojson: the geojson file of the country's map
48+
- locations: a list or numpy array of the indexs or names of the different regions from the csv file that you'd like to plot its data on the map, which should match the 'id' part of the geojson file
49+
- z: the data of different regions that you'd like plot from the csv file
50+
- colorscale: the type of colorbar that you'd like to use to plot the data, which can also be customized
51+
- zmin: the minimum value of z
52+
- zmax: the maxmum value of z
53+
- mapbox_center: the lattitude and longtitude of the country
54+
55+
## Outcome
56+
57+
The map that I drawed is as follows:
58+
59+
![Image of the Map](https://github.com/yg2619/Choropleth-Maps-in-Python/blob/master/choropleth-map-image.png)

0 commit comments

Comments
 (0)