8

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!

martineau
  • 119,623
  • 25
  • 170
  • 301
an jo
  • 83
  • 4
  • 1
    I think the issue is because of the color parameter. could you please share the part of the variables with me? `longitude_polyT` , `latitude_polyT` and `Mog` – Omid Roshani Feb 16 '22 at 07:41
  • Please post the full code so that we can run it and reproduce what you get. If there are any images involved, also post those in links. – Red Feb 17 '22 at 17:43

2 Answers2

5

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 :(

Dan M
  • 4,340
  • 8
  • 20
0

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