# pyGMT makecpt - background and foreground

**URL:** <https://forum.generic-mapping-tools.org/t/pygmt-makecpt-background-and-foreground/403>\
**Category:** PyGMT Q&A\
**Created:** [April 21, 2020, 12:57pm UTC](https://forum.generic-mapping-tools.org/t/pygmt-makecpt-background-and-foreground/403 "2020-04-21T12:57:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shicks-seismo](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@shicks-seismo](https://forum.generic-mapping-tools.org/u/shicks-seismo)\
**Post date:** [April 21, 2020, 12:57pm UTC](https://forum.generic-mapping-tools.org/t/pygmt-makecpt-background-and-foreground/403/1 "2020-04-21T12:57:14Z")

</div>

Hi, is there an equivalent` makecpt -D` option in pygmt for setting the background and foreground colours for the lowest and highest z-values? I couldn’t see any obvious usage information for this in `pygmt.makecpt`.  
(Apologies in advance if this is the wrong place to post pygmt questions).

---

<div class="post-metadata">

**Author:** ![mgrund](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mgrund/32/139_2.png) [@mgrund](https://forum.generic-mapping-tools.org/u/mgrund)\
**Post date:** [April 21, 2020, 3:14pm UTC](https://forum.generic-mapping-tools.org/t/pygmt-makecpt-background-and-foreground/403/2 "2020-04-21T15:14:38Z")

</div>

So far I used the `M` flag with an empty string which ensures that your fore and background colors defined e.g. via

```
pygmt.config(COLOR_BACKGROUND = 'white',
         COLOR_FOREGROUND = 'black')

```

are used:

`pygmt.makecpt(cmap = 'viridis', reverse = True, series = [0, 180, 10], output = outcpt.cpt', continuous = True, M = '')`

Hope that helps. I assume that in future the `M` flag may also be accessible via another alias.

---

<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:** [April 22, 2020, 12:05am UTC](https://forum.generic-mapping-tools.org/t/pygmt-makecpt-background-and-foreground/403/3 "2020-04-22T00:05:37Z")

</div>

Hi @shicks-seismo, yes, this is the place to ask questions! Sorry for the poor documentation at [pygmt.makecpt](https://www.pygmt.org/dev/api/generated/pygmt.makecpt.html), you should be able to pass the `D` argument directly into `makecpt`. This applies to any single character arguments you find in [`GMT`’s `makecpt`](https://docs.generic-mapping-tools.org/6.0/makecpt.html), even though it would be nice to use a proper alias for better readability (it’s a work in progress).

Just building on from @mgrund’s post, here’s an example of how to set the background/foreground colour. The following code turns areas below sea level (0m) to lightblue and areas above 3000m to red:

```python
import pygmt

grid = pygmt.datasets.load_earth_relief()

fig = pygmt.Figure()
with pygmt.config(COLOR_FOREGROUND="red", COLOR_BACKGROUND="lightblue"):
    pygmt.makecpt(
        cmap="batlow", series=[0, 3000, 10], continuous=True, M=True,
    )
fig.grdimage(grid=grid)
fig.savefig(fname="makecpt_bg_fg.png")
fig.show()

```

produces:

 ![PyGMT makecpt with background and foreground set](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/1X/1ea1535ea6c2e68c644bdb773e45c60fded2f687.png)

The [`-D`](https://docs.generic-mapping-tools.org/6.0/makecpt.html#d) argument would **only** set the background/foreground colour according to what’s found in the CPT. In the example above using [‘batlow’](http://www.fabiocrameri.ch/batlow.php), that would be a darkblue/brightpink sort of background/foreground colour which you can’t change. Using [`-M`](https://docs.generic-mapping-tools.org/6.0/makecpt.html#m) allows you to set those background/foreground colours manually.

Note that if you combine `-D` with `-M`, only `COLOR_NAN` is considered (useful for colouring blank pixels). My friend has a good (pure GMT) tutorial you can refer to at [https://gmt-tutorials.org/en/cpt\_colormap.html](https://gmt-tutorials.org/en/cpt_colormap.html) which shows what this looks like.
