Single side double stroke pen for contours?

I found a nice solution to a cosmetic problem that is bugging me for some time now but I don’t know how to reproduce it with GMT.

When drawing contour lines or just a basic grid the contrast between the line and the fill beneath it varies greatly with the color of the fill. Until now I simply did a double stroke: first a wider white pen and then overlay the black pen to get a contrast. Unfortunately that makes for visually very uneven contours:

See the screenshot below how they plotted the white line only on one side of the dark grey contour:

In my eyes that works surprisingly well for increasing the contrast of the contours in the darker areas while not giving it too much prominence over the contours in the lighter areas.

Any ideas how I can achieve this “single side double stroke” effect with GMT?

Some code to play with:

gmt begin contours jpg
	gmt grdimage -R11.13/46.79/11.15/46.8+r -JL11.14/46.795/46.79/46.8/12c @earth_relief -I -Cdarkgrey,white
	gmt grdcontour @earth_relief -C20 -A100 -Wa2.25p,white -Wc0.75p,white
	gmt grdcontour @earth_relief -C20 -A100 -Wa0.75p,grey35 -Wc0.25p,grey35
gmt end show

Seems too simple to work and shot from the hip: what about shifting the plot with -X and -Y?

Hi @Andreas that wouldn’t really work:


Right. A bit too easy.

This is not a solution to your problem, but I came across a similar problem in the domain of pure PostScript (no GMT), wanting to have a “half stroke” on a line. I asked on StackOverflow and Ture Pålsson suggested a clip-and-stroke technique which works for my use-case. Unlikely to be feasible in your case (it would need to be done outside pscontour, which would be painful).

But possibly this prompts other ideas?

That could be done by exporting all of the contours and then use psclip/clip on them … this might actually work. Thank you for the idea, @jjg

Hi Kristoff

You can obtain something similar to that one-sided double stroke using gmt plot with -Sf, using very close white boxes on one side of the line. You need to export first the contours to be plotted with gmt plot, of course, so it’s not the best solution, but it works.

    gmt grdcontour -C20 -A100 -Dcontours.dat
    gmt plot contours.dat -Sf1p/1.5p+b+l+p -Gwhite 
    gmt grdcontour -C20 -A100 -Wa0.75p,grey35 -Wc0.25p,grey35



Liable to be complicated since a PostScript clip uses a path, whereas (I believe) GMT contours are just lines … so you’d need to form paths from lines in some manner. Sounds like the type of thing better done in pscontour itself (but I’m not in a position to volunteer for that :smile:)

That is pretty clever @Jose-Alvarez! Thank you - I’ll toy around with it a bit.

Would simply drawing the white line at Z-delta work too?

Interesting, delta would vary spatially of course (depending on the gradients), perhaps one could do this with a slightly modified topography …

This is a great question!

A couple things weren’t actually noted about the screenshot example that Kristof is aiming for. One is that the white line side of the contour is always on the downhill side of the line. Also, it should be noted that the amount of white is wider on the labeled (primary) contours, than on the unlabeled contours.

Conceptually, the downhill gradient could be of some use, here, to provide information on the downhill direction (e.g., grdgradient -D option). If there was a way, say within grdmath, to combine that info to create a fixed-distance shift in the actual contour line, perhaps this would achieve the same effect.

I doubt that grdmath could handle it, because most of the operations are applicable only along the Z-axis, not the X,Y-axes, which would be required to accomplish this task. I hate to suggest adding an extension to grdmath that could be used on the “horizontal axes” as well as the vertical axis. I can see it now - GMT abusers trying to apply esoteric (e.g., transcendental or logarithmic functions) to the X,Y coordinates of their grids! That’ll open another can of worms! :snake:

Like Andreas, I’m shooting from the hip; and, like jjg said, I’m not sure it can all be accomplished within grd…-level functions. It may require going to the level of pscontour (which would be kind of ugly). Enough mulling over this one, a week too late, to boot.