# How to color polygons of a geopandas dataframe in pygmt?

**URL:** <https://forum.generic-mapping-tools.org/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138>\
**Category:** PyGMT Q&A\
**Created:** [December 18, 2020, 10:10pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138 "2020-12-18T22:10:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jamal](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jamal](https://forum.generic-mapping-tools.org/u/Jamal)\
**Post date:** [December 18, 2020, 10:10pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138/1 "2020-12-18T22:10:27Z")

</div>

Hi, I am trying to make a choropleth map from a shapefile using pygmt. I followed one answer on how to plot polygons using pygmt from geodataframe, but when I specify `color=gdf.aal_com` to color each polygon by column vasues I get an error:

> GMTInvalidInput: Can’t use arrays for color if data is matrix or file.

```
import geopandas as gpd
import pygmt
import os
#%% 
gdf = gpd.read_file('../Albania_loss.shp')
gdf.head()

```

| | hex\_id | pop | square\_pop | NAME\_0 | aal\_com | geometry |
| --- | --- | --- | --- | --- | --- | --- |
| 0 | 7310 | 9749.987529 | 98.742025 | Albania | 41208.410919 | POLYGON ((19.58093 40.88737, 19.52319 40.88737… |
| 1 | 8112 | 476.375406 | 21.826026 | Albania | 3317.674009 | POLYGON ((20.01394 40.13737, 19.95620 40.13737.. |

```
# collect extent region
bound = gdf.total_bounds
region = [bound[0], bound[2], bound[1], bound[3]]

# write to (tmp) file 
# !geopandas (fiona) is not able to overwrite this format
filename = '../albania.gmt'
gdf.to_file(filename, driver='OGR_GMT')

# plot
fig = pygmt.Figure()
fig.basemap(region=region, projection="M20c", frame=True)
fig.plot(data='albania.gmt',color=gdf.aal_com)
fig.show(method='external')

```

Is there another way to achieve like the following in pygmt?

 ![choro](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/1X/5c89bf898439634b35eecb52bc946894e93059a4.png)

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [December 19, 2020, 2:16am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138/2 "2020-12-19T02:16:58Z")

</div>

Choropleth maps are not easy to do in GMT. I’m able to do them from Julia but I use the **-Z** option and some helping functions to set the _zvals_. Not very helpful I know, sorry.

---

<div class="post-metadata">

**Author:** ![weiji14](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/weiji14/32/33_2.png) [@weiji14](https://forum.generic-mapping-tools.org/u/weiji14)\
**Post date:** [December 20, 2020, 7:20am UTC](https://forum.generic-mapping-tools.org/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138/3 "2020-12-20T07:20:51Z")

</div>

Hi @Jamal, you’re in luck, I recently had to do this as well (see [Coloring OGR\_GMT polygon files based on attribute column](https://flint.soest.hawaii.edu/t/coloring-ogr-gmt-polygon-files-based-on-attribute-column/1129/3)). Since you already have an OGR GMT file, you can try this:

```python
fig = pygmt.Figure()
fig.basemap(region=region, projection="M20c", frame=True)
pygmt.makecpt(cmap="batlow", series=[0, 50000, 100], reverse=True, continuous=True)
fig.plot(
    data="albania.gmt", # input OGR data file
    pen="0.05p,yellow,-", # set outline colour
    cmap=True, # use colormap from makecpt
    color="+z", # color based on the Z column
    close=True, # force close polygons
    a="Z=aal_com", # set aspatial column to 'aal_com'
)
fig.colorbar(position="jMR+jMR+w5c+o0.5c/1c+e")
fig.show()

```

Let us know if that works, might need to tweak a few things depending on your data file. You might find looking at [https://docs.generic-mapping-tools.org/latest/plot.html#g](https://docs.generic-mapping-tools.org/latest/plot.html#g) helpful.

P.S. We’re looking into integrating pygmt with geopandas at [https://github.com/GenericMappingTools/pygmt/issues/608](https://github.com/GenericMappingTools/pygmt/issues/608) so if you have any suggestions, feel free to leave a comment there!

---

<div class="post-metadata">

**Author:** ![Jamal](https://avatars.discourse-cdn.com/v4/letter/j/35a633/32.png) [@Jamal](https://forum.generic-mapping-tools.org/u/Jamal)\
**Post date:** [December 20, 2020, 10:31pm UTC](https://forum.generic-mapping-tools.org/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138/4 "2020-12-20T22:31:13Z")

</div>

> [@weiji14](#):
>
> `cmap="batlow", series=[0, 50000, 100]`

Thank you @weiji14, this is exactly what I was looking for!  
with code [adapted]

 ![output](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/1X/4add9e1e7095c6e9887630997b13c8418edaea56.png)
