Pygmt : how to fill polygons with cmap color

721 views Asked by At

Good morning,

I am using pygmt on python 3.6 with spyder. I am trying to fill several polygons in a range of colors defined by a colorpalet. I used makecpt to define the colorpalet. The variable I want to represent is Mog.

My code is :

pygmt.makecpt(cmap="bilbao",  series=[-5, 50, 5])

for i , long in enumerate(longitude_polyT):     
      fig.plot(x=longitude_polyT[i], y=latitude_polyT[i], frame="a", pen="black", color=Mog[i], cmap=True)

But it doesn't fill my polygons.

Does anybody have an idea about it?

Thanks a lot!

2

There are 2 answers

0
Dan M On BEST ANSWER

Here is my best guess at what you want:

import pygmt

fig = pygmt.Figure()
a = fig.basemap(region=[0, 6, 0, 2], projection="X12c/4c", frame=True, )

pol = ["n0.9c", "c0.9c", "d0.9c"]
Mog = [
    pygmt.makecpt(cmap="bilbao", series=[-5, 50, 5]),
    pygmt.makecpt(cmap="bilbao", series=[-5, 15, 5]),
    pygmt.makecpt(cmap="bilbao", series=[-8000, 4000])
]
longitude_polyT = [1, 3, 5]
latitude_polyT = [1, 1, 1]

for i, long in enumerate(longitude_polyT):
    fig.plot(x=long, y=latitude_polyT[i], style=pol[i], frame=a,
             pen="black",
             color=Mog[i], cmap=True)
    
fig.show()

enter image description here

Couldn't get it to show different colours :(

0
yvonnefroehlich On

To color a symbol based on a specific value, you may want to use the zvalue parameter together with color="+z" and cmap=True of the pygmt.Figure.plot method (for details please see the documentation at https://www.pygmt.org/latest/api/generated/pygmt.Figure.plot.html).

Please find below a code example, modified from the examples posted above. Even this question is already quite old, this answer maybe helps others with a similar question.

Code example

import pygmt

# Define variables
pol = ["n0.9c", "c0.9c", "d0.9c"]
lon_polyT = [1, 3, 5]
Mog = [10, -3, 20]

# Create figure object
fig = pygmt.Figure()

# Set up colormap
pygmt.makecpt(cmap="bilbao", series=[-5, 50, 5])

# Make basic map
fig.basemap(
    region=[0, 6, 0, 2],
    projection="X10c/4c",
    frame="a1fg1",
)

# Plot symbols
# https://www.pygmt.org/latest/api/generated/pygmt.Figure.plot.html
for i in range(len(lon_polyT)):
    fig.plot(
        x=lon_polyT[i],
        y=1,
        style=pol[i],  # symbole shape and size
        pen="1p,black",  # outline thickness in points and color
        fill="+z",  # apply to the fill | before PyGMT v0.8.0 "color"
        zvalue=Mog[i],  # value used for color-coding
        cmap=True,  # use colormap set up above
    )

# Show and save figure
fig.show()
# fig.savefig(fname="symbols_color_by_zvalue.png")

Output figure enter image description here