# How to create a 3D network with GMT.jl

**URL:** https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668
**Category:** Visualization
**Tags:** plotting, geo, gmt
**Created:** [May 11, 2023, 9:00am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668 "2023-05-11T09:00:22Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![gabipana7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabipana7/32/49689_2.png) [@gabipana7](https://discourse.julialang.org/u/gabipana7)
#### Post date: [May 11, 2023, 9:00am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668/1 "2023-05-11T09:00:22Z")

</div>

Hi everyone,  
I would like to create a 3D network using real coordinates of seismic centers. Without going too much into detail, I have a graph file of edgelists and I laso have the coordinates of the nodes. I am able to 3D plot the vertices with no problem as such:

```julia
scatter3(df_filtered_cubes.cubeLongitude, df_filtered_cubes.cubeLatitude, -df_filtered_cubes.cubeDepth,
frame="SEnwZ1+b xafg yafg zafg", proj=:merc, marker=:cube, markerline=:faint, markersize=0.1,
view=(135,20), alpha=30, show=true)

```

 ![test](https://global.discourse-cdn.com/julialang/original/3X/b/a/bab8dfb30d6f1e8a92b9d91214fd40e790697f7a.png)

The problem is when I want to connect the vertices in a specific way (given the graph edges). I tried numerous ways to do this but nothing works. I even tried a workaround by plotting recursively each edge, but for some reason, when calling the plot twice, the package does not plot on the same 3D plane, but it plots it 2D, under the original scatter!  
Here is how to replicate the issue:

```julia
cat > example.txt << EOF
0 0 0
1 1 1
2 2 0
3 3 1
EOF

```

then in julia

```julia
scatter3("./example.txt")
plot3d!("./example.txt", pen=:thinner, savefig="example.png", show=true)

```

The proper way to do this seems to use the F|conn|connection option for the plot, but I cannot seem to figure out how to properly do this. I found this [answer](https://discourse.julialang.org/t/combine-makie-with-gmt-jl/74347/4) to a similar problem (but the person tries to plot in 2D, which is easier).

If anyone would be able to offer the proper way to pass the connection option for the plot, that would be much appreciated!

Thank you in advance!

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [May 11, 2023, 12:31pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668/2 "2023-05-11T12:31:44Z")

</div>

Hi,  
Yes, the `F|conn|connection` option does not exist in the 3D case. You may ask in the GMT forum on how hard would it be to add that option to `plot3d`

The manual way works but you had the bad luck of falling onto a bug.  
In the next I’ll use the `Vd` debugging option to show what is actual GMT command that is generated

```julia
t = [0 0 0; 1 1 1; 2 2 0; 3 3 1];

scatter3(t, Vd=1)
        psxyz -JX14c/9.5c -Baf -Bza -p200/30 -R-0.1/3.1/-0.1/3.1/0/1 -JZ6c -Su2p -P -K > c:\TMP/GMTjl_j.ps

# The bug is that the vertical scale/size (JZ6c) is not going to be transmitted, so we'll need to add it manually.

plot3d!(t, pen=:thinner, JZ="6c", show=true)

```

Now, to the real case. You have two options.

1- In a single file separate the edges with lines with 3 NaN  
2- Make that file a multi-segment file, where the different segments are separated with one line with only the “\>” character

Second form is preferable as when you import that file with `gmtread("file")` you get a Vector of GMTdatset segments with both individual and a global BoundingBox that helps to automatically set the vertical scales. And see also the `mat2ds` docs.

---

<div class="post-metadata">

### Author: ![gabipana7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabipana7/32/49689_2.png) [@gabipana7](https://discourse.julialang.org/u/gabipana7)
#### Post date: [May 11, 2023, 1:36pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668/3 "2023-05-11T13:36:56Z")

</div>

Thanks for the help, adding `JZ="6c" ` works great for now! Thank you also for the other 2 options, I will check them out.

On the other hand, I have 3 more questions if you could kindly help me with.

1. Is it possible to have the 3D axes bound separately (as you would do with initializing a basemap first). This would be good because if I choose to give an alpha value to my scatterplot, the axes also become transparent!
2. Is it possible to somehow give different alpha values to different points (by using a vector of values corresponding to different alphas). I think this is not possible, but still I ask
3. How would one place the colorbar on the vertical, along the Z axis, in a 3D plot. Mine bounds to the 2D base of the plot.

See the code to replicate the behaviors listed (the transparency of the axes and the misplaced colorbar):

```julia
plot3d(df_filtered_cubes.cubeLongitude, df_filtered_cubes.cubeLatitude, -df_filtered_cubes.cubeDepth,
frame="SEnwZ1+b xafg yafg zafg",proj=:merc, marker=:cube, markerline=:faint, markersize=marker_size,
cmap=C_markers, zcolor=connectivity, 
alpha=70, view=(135,20))

colorbar!(pos=(outside=:BC, offset=(0,1.5)), shade=0.4, xaxis=(annot=:auto,), frame=(xlabel="Degree",),par=(MAP_LABEL_OFFSET=0.8,), savefig="test.png", show=true)

```

 ![test](https://global.discourse-cdn.com/julialang/original/3X/c/2/c2e7922c63a7d464ca73e709489880b546e70c9a.png)

If I try to put a basemap, only the bottom plane gets pasted in and it also covers the plot!

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [May 11, 2023, 3:01pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668/4 "2023-05-11T15:01:16Z")

</div>

I fixed that bug in master plus another where `basemap` was not parsing the vertical scale option, but since you are not using it because your are passing the condensed form `frame="SEnwZ1+b xafg yafg zafg"` that should work on basemap to. So you can avoid the undesired transparency on axes by calling `basemap` first (but then you must provide the limits too).

It must be possible to assign different transparencies to each cube. One way, and this is only from memory, it to create a color map (the CPT) such that the colors have transparencies too (need to dig the GMT CookBook on how to do that). The other is to create a multi-segment file, with one point per segment and assign it different colors and transparencies to each point. The `fillalpha, is3D and multicol` options of the `mat2ds` function should help with that.

The colorbar issue is another thing that I would like that we had automatically in GMT but we don’t (you are very welcome to open a feature request for that). The solution here is to use the more cumbersome manual positioning using paper coordinates with `position(paper=true, anchor=(xx,yy), ...)`

---

<div class="post-metadata">

### Author: ![gabipana7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabipana7/32/49689_2.png) [@gabipana7](https://discourse.julialang.org/u/gabipana7)
#### Post date: [May 12, 2023, 10:10am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668/5 "2023-05-12T10:10:34Z")

</div>

Sorry to bother you, but I am extremely new to GMT, even to Julia to some extent.

I do not get how I should pass the Z axis on the basemap, I have tried using a combination of Julia and GMT but nothing works.

> So you can avoid the undesired transparency on axes by calling `basemap` first (but then you must provide the limits too).

Mind you, even that frame=“…” took me a while to get, I do not seem figure out how to provide the limits, so that the zaxis shows properly!

Also the colorbar anchoring seems to not work,  
I’ve tried

```julia
colorbar!(pos=(paper=(2.5,2.5), size=(10,1.25), vertical=true),show=true)

```

and it still shows on the bottom plane.  
But this is not that important to me as the controling of the axes is.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [May 12, 2023, 7:04pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668/6 "2023-05-12T19:04:28Z")

</div>

> [@gabipana7](#):
>
> I do not get how I should pass the Z axis on the basemap,

Sorry, busy day and it’s not over yet. I intend to make some changes that will not apply the transparency to the axes and so this problem will disappear. But meanwhile you can create a Z axis with the “za” flags like in the bellow example. I don’t like that the default does not save us from having to do this (another thing to fix) and the verbose answer (specifying each of the 3 axis separately) would make a bit long to my taste here.

```julia
basemap(limits=(-0.1,3.1,-0.1,3.1,0,1), zsize=6, frame="af za", view=(200,30), show=1)

```

> [@gabipana7](#):
>
> Mind you, even that frame=“…” took me a while to get

Well, all of those aliases are explained in the documentation and used extensively in the examples.  
It’s either `frame`, or `B`. Second is shorter but much less comprehensible for the non-initiated  
in GMT.

> [@gabipana7](#):
>
> But this is not that important to me as the controling of the axes is

I’ll try to come with a simple colorbar example later tonight (or tomorrow) and meanwhile, for the axes control, see the [examples here](https://www.generic-mapping-tools.org/GMTjl_doc/examples/frames/) (and the docs ofc). You’ll find that you can control everything you want.

---

<div class="post-metadata">

### Author: ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)
#### Post date: [May 13, 2023, 6:40pm UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668/7 "2023-05-13T18:40:41Z")

</div>

If you update to master the transparency will no longer applies to the axes, no need to resort to a previous `basemap` call (in fact doing this under the hood is what I implemented).

Regarding the `colorbar!`, the issue here is that when one one uses the `!` version it inherits the fig limits, perspective, etc, from the previous call and we don’t want that in this case. So we must pass those explicitly. _e.g._

```julia
t = [0 0 0; 1 1 1; 2 2 0; 3 3 1];
scatter3(t, t=50)
colorbar!(region=(0,21,0,29), pos=(paper=(18,2.0), size=(8,1)), cmap=:jet, view=(180,90), show=true)

```

---

<div class="post-metadata">

### Author: ![gabipana7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gabipana7/32/49689_2.png) [@gabipana7](https://discourse.julialang.org/u/gabipana7)
#### Post date: [May 14, 2023, 9:57am UTC](https://discourse.julialang.org/t/how-to-create-a-3d-network-with-gmt-jl/98668/8 "2023-05-14T09:57:13Z")

</div>

Thank you very much for your help!  
Based on your suggestions and fixes I’ve been able to create the plots that I require, exactly how I wanted them.
