# Problems with contours on global maps

**URL:** <https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393>\
**Category:** Q&A\
**Tags:** gmt\
**Created:** [September 27, 2024, 3:01pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393 "2024-09-27T15:01:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrak](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mrak/32/27_2.png) [@mrak](https://forum.generic-mapping-tools.org/u/mrak)\
**Post date:** [September 27, 2024, 3:01pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/1 "2024-09-27T15:01:23Z")

</div>

I am trying to create a contour of a misfit function on a global map where the minimum misfit is close to the pole. As you can see in the plot, the contour is plotted correctly, but it draws a line connecting the two points on the left and right of the figure. Does anyone know of a way to fix this?

This image was created with pygmt, but I suspect that this is also a problem with gmt itself.

 ![Screenshot from 2024-09-27 17-02-41](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/4/42a4121441c1dc8d057560e390e47d2bab098005.jpeg)

---

<div class="post-metadata">

**Author:** ![espiritocz](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/espiritocz/32/904_2.png) [@espiritocz](https://forum.generic-mapping-tools.org/u/espiritocz)\
**Post date:** [September 27, 2024, 6:26pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/2 "2024-09-27T18:26:00Z")

</div>

hi, please add your code

---

<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:** [September 27, 2024, 7:15pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/3 "2024-09-27T19:15:30Z")

</div>

These are hard bugs to find. We know of a few circumstances where it happens. Several of these had been fixed in the past but some cases insist in resisting.

Can you please dump that contour and attach it to this post?

---

<div class="post-metadata">

**Author:** ![mrak](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mrak/32/27_2.png) [@mrak](https://forum.generic-mapping-tools.org/u/mrak)\
**Post date:** [September 30, 2024, 9:50pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/4 "2024-09-30T21:50:30Z")

</div>

It turns out that this in fact works correctly with gmt, and that the problem is with pygmt.

In pygmt, the grdcontour method works fine if you pass the name of a netcdf file. However, when passing an xarray dataarray, this is when you get the odd behaviour shown above.

I’ll try to come up with a reproducible example and then open an issue with them.

---

<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:** [September 30, 2024, 10:19pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/5 "2024-09-30T22:19:28Z")

</div>

In the end, GMT is still the one to blame. My guess is that a pixel _vs_ grid registration difference is what is what triggers the bug via pygmt. Again, can you dump that contour with both pyGMT and plain GMT and see if how they differ?

---

<div class="post-metadata">

**Author:** ![mrak](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/mrak/32/27_2.png) [@mrak](https://forum.generic-mapping-tools.org/u/mrak)\
**Post date:** [October 1, 2024, 8:25pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/6 "2024-10-01T20:25:37Z")

</div>

Here is the code used in ipython to (1) create an xarray datarray using sphinterpolate, (2) export this as a netcdf file, and (3) export the contour using a gmt shell command:

```auto
da = pygmt.sphinterpolate(np.array([lon, lat, misfit]).T, spacing=0.2, region='g')
da.to_netcdf('test.nc')
!gmt grdcontour test.nc -C0.40494052, -D > contour.dat

```

This shows that the dataarray is by default using pixel registration and geographic coordinates

```auto
In [92]: da.gmt.registration, da.gmt.gtype
Out[92]: (0, 1)

```

And here is the contour file  
[contour.dat](https://forum.generic-mapping-tools.org/uploads/short-url/oxX6vmZqxNW2QyJ0zAsSN9lXWAL.dat) (64.8 KB)

And for completeness, here are the two ways of creating the pygmt image, the first doesn’t work, the second does:

```auto
fig = pygmt.Figure()
fig.grdimage(da, projection='W0/10c')
fig.grdcontour(da, levels='+0.40494052')
fig.show()

fig2 = pygmt.Figure()
fig2.grdimage('test.nc', projection='W0/10c')
fig2.grdcontour('test.nc', levels='+0.40494052')
fig2.show()

```

And here is the pygmt issue: [problems with grdcontour when using xarray DataArrays · Issue #3479 · GenericMappingTools/pygmt · GitHub](https://github.com/GenericMappingTools/pygmt/issues/3479)

---

<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:** [October 1, 2024, 9:32pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/7 "2024-10-01T21:32:50Z")

</div>

> [@mrak](#):
>
> This shows that the dataarray is by default using pixel registration and geographic coordinates
> 
> ```auto
> In [92]: da.gmt.registration, da.gmt.gtype
> Out[92]: (0, 1)
> 
> ```

Nope. `0` means grid registration.

---

<div class="post-metadata">

**Author:** ![MarkWieczorek](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/markwieczorek/32/392_2.png) [@MarkWieczorek](https://forum.generic-mapping-tools.org/u/MarkWieczorek)\
**Post date:** [October 2, 2024, 8:51am UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/8 "2024-10-02T08:51:32Z")

</div>

You are right. That was a typo. In any case, it has no influence on the underlying problem. You get the same result setting it to 0 or 1.

---

<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:** [October 2, 2024, 3:58pm UTC](https://forum.generic-mapping-tools.org/t/problems-with-contours-on-global-maps/5393/9 "2024-10-02T15:58:55Z")

</div>

> [@MarkWieczorek](#):
>
> You get the same result setting it to 0 or 1.

But you don’t set it to 0 or 1. What determines its value is if the grid is pixel or gridline registered, and on the PyGMT issue I see these **important** warning messages that clearly indicate that there is an error in creating that grid

```auto
grdimage [WARNING]: Guessing of registration in conflict between x and y, using gridline
grdcontour [WARNING]: Guessing of registration in conflict between x and y, using gridline

```

Plotting that contour alone works just fine.

```auto
plot contour.dat -Rd -JW0/10c -Baf -BWSen -png lixo

```
