# Ternary plots in GMT.jl

The ternary plots topic has been raised here several times. I’ve added ternary plots to GMT.jl. Unfortunately it’s still not yet possible to slant the ticks, which helps a lot in reading the graphs, as that it will have to wait until GMT itself can do it.

Several examples are shown here and a quick example can be obtained with:

``````ternary("@ternary.txt", labels=("Clay","Silt","Sand"), image=true, marker=:p, show=true)
``````

8 Likes

These look great! Thanks

Can GMT.jl produce a ternary plot with lines by (smoothly) connecting given points? Note it is neither scatter plot nor contour plot, such as the plot below:

Interested also on how to do this with GMT.

Fyi, it is already possible to do something like it using TernaryPlots.jl, a package that seems to be at early stages:

The corresponding code is provided below:

``````using TernaryPlots

b = 0:0.01:0.3
c1 = (1 .- b).^3 .- 0.7^3
c2 = (1 .- 2*b).^2 .- 0.4^2
t1 = tern2cart.(1 .- b .- c1, b, c1)
t2 = tern2cart.(1 .- b .- c2, b, c2)

p = TernaryPlots.ternary_plot(title="TernaryPlots.jl",labels=(A="A", B="B", C="C"))
plot!(p, [t1...], lw=2, lc=:red)
plot!(p, [t2...], lw=2, lc=:blue)
``````
1 Like

OK, normally the ternary module in GMT (the C package) does not allow plotting lines. But symbols in GMT can be lots of things, including entities called decorated lines (see this example) and we can make use of them to plot lines.

You can now plot lines using this trick, but because it’s a trick the hard core GMT syntax must be used. Another problem I found is that apparently `ternary` in GMT.jl requires input data via a file. So please open an issue in GMT.jl requesting a proper friendly syntax to do this task. So far you’ll have to do this.

Save data in a text file (for example put this matrix [0.1 0.4 0.5; 0.2 0.0 0.8] in a file called “tern.dat”, one row per line) and use it as

``````ternary("tern.dat",  S="~n1:+sc0.01", par=(MAP_DEFAULT_PEN="1,red",), show=true)
``````
• The S=… is the trick to make a decorated line but one with an invisible small circle
• the `par=(MAP_DEFAULT_PEN="1,red",)` tells it to plot a red line of 1p thickness. Omitting this option plots a 0.25p black line (the GMT default)

2 Likes

Thanks! I find the API of TernaryPlots.jl is more pleasing and the look is great.

Me too, and that’s why I said.

It turns out that a good improvement can be achieved with little changes, though I still need to find why it errors when one tries to add a Title.

With GMT.jl#master we can now expand a bit Rafael’s example

``````b = 0:0.01:0.3
c1 = (1 .- b).^3 .- 0.7^3
c2 = (1 .- 2*b).^2 .- 0.4^2
t1 = tern2cart([(1 .- b .- c1) b c1])    # Note that GMT.jl function expects a Mx3 matrix
t2 = tern2cart([(1 .- b .- c2) b c2])

ternary(labels=("A", "B", "C"))

plot!(t1, lw=2, lc=:red, ls="line& (a) &")	# line style -> fancy new stuff
plot!(t2, lw=2, lc=:blue)
text!(tern2cart([0.3 0.4 0.3]), text="Umbilicus", font=18, show=true)
``````

3 Likes