# Unable to color polygons based on their attributes

**URL:** <https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135>\
**Category:** Q&A\
**Created:** [July 19, 2022, 12:28pm UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135 "2022-07-19T12:28:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![arindan](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/arindan/32/2341_2.png) [@arindan](https://forum.generic-mapping-tools.org/u/arindan)\
**Post date:** [July 19, 2022, 12:28pm UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135/1 "2022-07-19T12:28:50Z")

</div>

Hi everyone,  
I am facing issue while trying to color polygons based on their attributes.  
I tried to do this through this method ([https://flint.soest.hawaii.edu/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138](https://flint.soest.hawaii.edu/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138)), but it still not solved.

I am trying to color the polygons based on the column ‘Year\_only’ (in shapefile). In case anyone would like to see it: [https://easyupload.io/nq657l](https://easyupload.io/nq657l) (here is the shapefile that I used; it a small file).

Here are the codes I used:

```auto
fig = pygmt.Figure()
pygmt.makecpt(cmap="vik", series=[2017, 2021, 1], continuous=True, reverse=True)
fig.plot(data=footprint_each_DEMs,
         cmap=True, 
         color="+z", 
         close=True, 
         aspatial="Z=Year",
         pen="thinnest,red,-"
        )
fig.colorbar(position="jMR+jMR+w8c+o0.1c/1c+e")
fig.show()

```

I followed the same method as in the example ([https://flint.soest.hawaii.edu/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138](https://flint.soest.hawaii.edu/t/how-to-color-polygons-of-a-geopandas-dataframe-in-pygmt/1138)), but It seems not able to get what I expect.

What I got is attached below. The polygons are not colored based on the attributes, though the right colorbar appears.

 ![footprints](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/6/6457e4e93654a4461a9c18bbd88fd409ef2c0f8d.png)

But I expect something like below, with Years as colorbar.

 ![index](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/7/77595b351eaa094e878179211be97ad561101ded.png)

It would be great if anyone can give me some hint to get so.

Thank you.  
Arindan

---

<div class="post-metadata">

**Author:** ![mkononets](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mkononets/32/4029_2.png) [@mkononets](https://forum.generic-mapping-tools.org/u/mkononets)\
**Post date:** [July 19, 2022, 3:39pm UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135/2 "2022-07-19T15:39:18Z")

</div>

> [@arindan](#):
>
> I am trying to color the polygons based on the column ‘Year\_only’ (in shapefile).

> [@arindan](#):
>
> `aspatial="Z=Year"`

should it not be `Year_only` then?

---

<div class="post-metadata">

**Author:** ![arindan](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/arindan/32/2341_2.png) [@arindan](https://forum.generic-mapping-tools.org/u/arindan)\
**Post date:** [July 19, 2022, 4:36pm UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135/3 "2022-07-19T16:36:31Z")

</div>

Hi Mikhail,  
Thanks for seeing this.  
Even if I use `aspatial="Z=Year"`, It doesn’t appear what I expected. `aspatial="Z=Year"` it has also the date attributes.

Or I think there is some other issue or I am doing some mistake.

---

<div class="post-metadata">

**Author:** ![mkononets](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mkononets/32/4029_2.png) [@mkononets](https://forum.generic-mapping-tools.org/u/mkononets)\
**Post date:** [July 19, 2022, 4:53pm UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135/4 "2022-07-19T16:53:25Z")

</div>

you should try `aspatial="Z=Year_only"`

---

<div class="post-metadata">

**Author:** ![arindan](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/arindan/32/2341_2.png) [@arindan](https://forum.generic-mapping-tools.org/u/arindan)\
**Post date:** [July 20, 2022, 3:34am UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135/5 "2022-07-20T03:34:34Z")

</div>

Hi Mikhail,

Thanks.  
`aspatial="Z=Year_only"` did not work. It gives the same plot with red outlines, as shown in my first comment.

Is it something like the issue with the pen color?

Arindan

---

<div class="post-metadata">

**Author:** ![mkononets](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mkononets/32/4029_2.png) [@mkononets](https://forum.generic-mapping-tools.org/u/mkononets)\
**Post date:** [July 20, 2022, 7:40am UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135/6 "2022-07-20T07:40:59Z")

</div>

```auto
import pygmt
filename = 'footprint/footprint_each_DEMs.shp'

fig = pygmt.Figure()
pygmt.makecpt(cmap="vik", series=[2017, 2021, 1], continuous=True, reverse=True)
fig.plot(data=filename,
         cmap=True, 
         color='+z', 
         close=True, 
         aspatial='Z=Year_only',
         pen="thinnest,red,-"
        )
fig.colorbar(position="jMR+jMR+w8c+o0.1c/1c+e")
fig.show()

```

 ![test](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/0/047a4789ff350e26bd5e26a387a257dfa21511cc.png)

---

<div class="post-metadata">

**Author:** ![arindan](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/arindan/32/2341_2.png) [@arindan](https://forum.generic-mapping-tools.org/u/arindan)\
**Post date:** [July 20, 2022, 8:32am UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135/7 "2022-07-20T08:32:48Z")

</div>

Hi Mikhail,

Thanks for this. I was looking for this.  
What was the mistake in my code is: I was using `geopandas` to open the shapefile.  
Like: `footprint_each_DEMs = gpd.read_file(topdir + '/Footprint of dh/footprint_each_DEMs.shp')`

Thanks for giving it a try and the solution.

Best, Arindan

---

<div class="post-metadata">

**Author:** ![mkononets](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mkononets/32/4029_2.png) [@mkononets](https://forum.generic-mapping-tools.org/u/mkononets)\
**Post date:** [July 20, 2022, 6:00pm UTC](https://forum.generic-mapping-tools.org/t/unable-to-color-polygons-based-on-their-attributes/3135/8 "2022-07-20T18:00:26Z")

</div>

I actually have no idea how to do it using `geopandas`

I would be glad to learn if somebody who knows could explain it to us.
