Plot a choropleth map with a geojson file.
Project description
choropleth-geojson
The module was inspired by the work presented on http://vincepota.com/plotly_choropleth_map.html and also on https://github.com/vincepota/plotly_choropleth_tutorial.
The module enables the plotting of a choropleth map from a compatible geojson file. To check for the compatibility of a geojson file, the file can be uploaded to http://geojson.io/#map=2/20.0/0.0. If the area of the map that corresponds to the coordinates on the geojson file is shaded, then the geojson file is compatibile to be used with this module. Refer to the end of the document for an example of a compatible geojson file. The module also requires an API key from Mapbox. An API key can be obtained for free from https://docs.mapbox.com/help/how-mapbox-works/access-tokens/.
A choropleth map is a heatmap-like geographical plot. It displays a region of interest with subregions colored. The color intensity is determined by the magnitude of the data of interest in each subregion. For example, say the data of interest is the human population of each country with the region of interest being the North American continent, and the subregions are the countries present in that continent. A choropleth map will shade the countries with varying shades of red (or a different color of choice) that represent the magnitude of the population level in each country. This can range from light red to the least populous country to dark red to the most populous country.
Suppose that the population data is saved to one of the columns in a pandas dataframe, with index representing each country, and there is a compatible geojson file that represents the coordinates of each country. Then, a minimal working example to plot the choropleth map is as follows,
import choropleth_geojson as cg
import pandas as pd
import json
import plotly.offline as offline
apikey = 'my_mapbox_apikey123alpha890'
df = pd.read_csv('population_data.csv', index_col = 0)
with open(r'north_america.json') as f:
geojson = json.load(f)
northamerica = cg.choropleth(apikey, df, geojson, 'Country')
fig = northamerica.choroplot()
offline.plot(fig, auto_open=True)
More examples can be found on https://github.com/QuantStats/choropleth_geojson or alternatively click on the homepage button to navigate to the GitHub repository.
An example of a compatible geojson file is as follows, also note that only geometry type of "Polygon" or "Multipolygon" is supported for this module,
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Subregion1"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-75.9375,
36.87962060502676
],
[
-82.265625,
37.16031654673677
],
[
-82.08984375,
33.578014746143985
],
[
-75.9375,
36.87962060502676
]
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "Subregion2"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-81.9580078125,
26.43122806450644
],
[
-81.4306640625,
25.60190226111573
],
[
-80.68359375,
26.15543796871355
],
[
-81.9580078125,
26.43122806450644
]
],
[
[
-81.9140625,
27.449790329784214
],
[
-82.3974609375,
27.0982539061379
],
[
-81.6064453125,
26.82407078047018
],
[
-81.9140625,
27.449790329784214
]
]
]
]
}
}
]
}
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file choropleth_geojson-0.0.7.tar.gz
.
File metadata
- Download URL: choropleth_geojson-0.0.7.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 824a16878fa56c581ce082d0e3229c6b564f7bb8b83ae4d557eb1538bf70249a |
|
MD5 | 1586e375f6d057235552b82effd19528 |
|
BLAKE2b-256 | d9d8e7b486cbde2bb1b35fb10c9d2342b94e8b235eaa9bb005d7682ef48e872e |
File details
Details for the file choropleth_geojson-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: choropleth_geojson-0.0.7-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46dd447b761d5e3490f2976f8533ac0bd2dc9cb083a3c8c74751437e6656a5b8 |
|
MD5 | 08b937ced5d7c8d50ef8e9e8b88f6512 |
|
BLAKE2b-256 | c7fb98134e0de537079c748988d4386df8c86c5d3449ac1a1efe1a0a399b786f |