# Plot a shapefile (.shp) in a Choropleth chart

**URL:** <https://community.plotly.com/t/plot-a-shapefile-shp-in-a-choropleth-chart/27850>\
**Category:** Dash Python\
**Created:** [August 27, 2019, 10:41am UTC](https://community.plotly.com/t/plot-a-shapefile-shp-in-a-choropleth-chart/27850 "2019-08-27T10:41:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmacedo](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/fmacedo/32/7790_2.png) [@fmacedo](https://community.plotly.com/u/fmacedo)\
**Post date:** [August 27, 2019, 10:41am UTC](https://community.plotly.com/t/plot-a-shapefile-shp-in-a-choropleth-chart/27850/1 "2019-08-27T10:41:39Z")

</div>

For people having a hard time creating a layer from a shapefile (.shp) and plotting it over a Choroplethmapbox chart, here’s one successful way of doing it:

1 - Install geopandas (This can be tricky, it’s easier if you use Anaconda, otherwise you have to install some components manually with pip - [python - how to successfully install pyproj and geopandas? - Stack Overflow](https://stackoverflow.com/questions/34427788/how-to-successfully-install-pyproj-and-geopandas)). geopandas is helpful to read a shapefile as a pandas DataFrame.

2 - read your shapefile into a Pandas DataFrame:

```python
    import geopandas as gpd
    geodf = gpd.read_file('path_to_shp_file')

```

3 - Create a GeoJSON file and read it:

```python
    geodf.to_file("path_to_GeoJSON _file", driver = "GeoJSON")
    with open("path_to_GeoJSON _file") as geofile:
    j_file = json.load(geofile)

```

So now you have successfully converted a shapefile into an appropriate format (GeoJSON) for plotly to read.

4 - **IMPORTANT** - When creating the plot You’ll need to have a feature-id associated with each feature, so you can associate the GeoJSON-formatted geometry with the _z_ attribute ([Mapbox Choropleth Maps | Python | Plotly](https://plot.ly/python/mapbox-county-choropleth/)). If you did not have an id in the first place, you can add it, since this j\_file is basically a big dict (there’s probably a better way of doing this though):

```python
    i=1
    for feature in j_file["features"]:
        feature ['id'] = str(i).zfill(2)
        i += 1

```

5 - Just follow the example instructions ([Mapbox Choropleth Maps | Python | Plotly](https://plot.ly/python/mapbox-county-choropleth/)) and add data indexed by the same _‘id’_ as the _j\_file_. the _locations_ attribute of the `Choroplethmapbox` chart should be a list (or a column of a pandas DataFrame) with the same values as the the _‘id’_ key values in the _j\_file_. The _z_ attribute should have the values in the same order.

---

<div class="post-metadata">

**Author:** ![empet](https://avatars.discourse-cdn.com/v4/letter/e/977dab/32.png) [@empet](https://community.plotly.com/u/empet)\
**Post date:** [August 27, 2019, 4:27pm UTC](https://community.plotly.com/t/plot-a-shapefile-shp-in-a-choropleth-chart/27850/2 "2019-08-27T16:27:55Z")

</div>

Here [https://plot.ly/~empet/15238](https://plot.ly/~empet/15238) is a step by step conversion of a shapefile to a geojson dict to be used in the definition of a `Choroplethmapbox`.

---

<div class="post-metadata">

**Author:** ![allenite](https://avatars.discourse-cdn.com/v4/letter/a/ba8739/32.png) [@allenite](https://community.plotly.com/u/allenite)\
**Post date:** [September 17, 2020, 10:42pm UTC](https://community.plotly.com/t/plot-a-shapefile-shp-in-a-choropleth-chart/27850/3 "2020-09-17T22:42:13Z")

</div>

Outstanding. Can’t say enough thanks for this.
