# Inconsistent plot between Julia matrix and GMT grid

**URL:** <https://forum.generic-mapping-tools.org/t/inconsistent-plot-between-julia-matrix-and-gmt-grid/2665>\
**Category:** GMT.jl Q&A\
**Created:** [February 26, 2022, 9:26am UTC](https://forum.generic-mapping-tools.org/t/inconsistent-plot-between-julia-matrix-and-gmt-grid/2665 "2022-02-26T09:26:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cuda-Chen](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/cuda-chen/32/2060_2.png) [@Cuda-Chen](https://forum.generic-mapping-tools.org/u/Cuda-Chen)\
**Post date:** [February 26, 2022, 9:26am UTC](https://forum.generic-mapping-tools.org/t/inconsistent-plot-between-julia-matrix-and-gmt-grid/2665/1 "2022-02-26T09:26:55Z")

</div>

I was trying to plot GMT grid, but I found the plots between Julia matrix and GMT grid are inconsistent (for clarity, the plot using Julia matrix is the plot I am desire for). So I would like to ask which parameters I should use in `xyz2grd` in order to produce the plot I want?

For your ease, here are the codes and plots:

- Julia Matrix

```julia
using GMT 
using DelimitedFiles

data_file = "./pdf_out.txt"

data = readdlm(data_file)
height, width = size(data)
grdview(data, J="X6i/5i", 
        xaxis=(annot=:auto, ticks=:auto, label="log10(Period)",), 
        yaxis=(annot=:auto, ticks=:auto, label="Power [10log10(m **2/sec** 4/Hz)] [dB]",),
        surftype=:surface, plane=(0, :white),
        color=:rainbow, V=true, S=100, Y=4.0,
        show=true)

```

 ![matrix](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/e/e641d33d2437986fa831fd893f73baa6f17a5380.png)

- GMT grid

```julia
using GMT
using DelimitedFiles

data_file = "./pdf_out.txt"

data = readdlm(data_file)
height, width = size(data)

data = reshape(data, :) # flatten the array to 1D
data_grid = xyz2grd(data, R="1/$width/1/$height", I="1/1", Z="TLA")
zmin, zmax = extrema(data)
g_cpt = makecpt(color=:rainbow, T="$zmin/$zmax")
grdview(data_grid, J="X6i/5i", color=g_cpt, surftype=:surface, plane=(0, :white), 
        xaxis=(annot=:auto, ticks=:auto, label="log10(Period)",), 
        yaxis=(annot=:auto, ticks=:auto, label="Power [10log10(m **2/sec** 4/Hz)] [dB]",),
        S=100, Y="4.0", show=true)

```

 ![grid](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/5/554929e8434d44ece1cfe61c2471432f66db1505.png)

I also provide the input file (`pdf_out.txt`) for reproducing this issue:  
[pdf\_out.txt](https://forum.generic-mapping-tools.org/uploads/short-url/71KxkZrBk2nieBP1FbYa87A8JUV.txt) (136.8 KB)

---

<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:** [February 26, 2022, 7:33pm UTC](https://forum.generic-mapping-tools.org/t/inconsistent-plot-between-julia-matrix-and-gmt-grid/2665/2 "2022-02-26T19:33:02Z")

</div>

Hi @Cuda-Chen, welcome here,

From the the `xyz2grd` manual and assuming your matrix is column major oriented and from bottom up you should use **Z=LBA** but the easiest is that you use the `mat2grid` function to convert the matrix into a grid. And you can also simplify the plot command by using `grdimage` instead of `grdviewd` (3D) to make a 2D plot.

_e.g._ (with the default _turbo_ colorscale)

```auto
data = readdlm("pdf_out.txt");
G = mat2grid(data);
grdimage(G, xaxis=(annot=:auto, ticks=:auto, label="log10(Period)"),
    yaxis=(annot=:auto, ticks=:auto, label="Power [10log10(m **2/sec** 4/Hz)] [dB]"),
    show=true)

```

---

<div class="post-metadata">

**Author:** ![Cuda-Chen](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/cuda-chen/32/2060_2.png) [@Cuda-Chen](https://forum.generic-mapping-tools.org/u/Cuda-Chen)\
**Post date:** [February 27, 2022, 6:04am UTC](https://forum.generic-mapping-tools.org/t/inconsistent-plot-between-julia-matrix-and-gmt-grid/2665/3 "2022-02-27T06:04:56Z")

</div>

Hi @Joaquim,

Thanks for your help, I can not only plot the figure with content identical to Julia matrix scene, but also  
get the usage of `mat2grid`. What’s more, the `grdimage` function really saves me the day!

Of course, I put the code and plot of `grdview` as well as `grdimage` for others who will search the result in the future:

- `grdview`

```julia
data = readdlm("pdf_out.txt")
data_grid = mat2grid(data)
grdview(data_grid, J="X6i/5i", color=:rainbow, surftype=:surface, plane=(0, :white), 
    xaxis=(annot=:auto, ticks=:auto, label="log10(Period)",), 
    yaxis=(annot=:auto, ticks=:auto, label="Power [10log10(m **2/sec** 4/Hz)] [dB]",),
    S=100, Y="4.0", show=true)

```

 ![matrix](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/e/e641d33d2437986fa831fd893f73baa6f17a5380.png)

- `grdimage`

```julia
grdimage(data_grid, J="X6i/5i", xaxis=(annot=:auto, ticks=:auto, label="log10(Period)"),
    yaxis=(annot=:auto, ticks=:auto, label="Power [10log10(m **2/sec** 4/Hz)] [dB]"),
    color=:rainbow,
    show=true)

```

 ![grdimage](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/f/fbd00a4ed7bcad3cf0c6e66e6ff196970c3beb42.png)

At last, thanks for your help!

---

<div class="post-metadata">

**Author:** ![LIGAbaji](https://avatars.discourse-cdn.com/v4/letter/l/6bbea6/32.png) [@LIGAbaji](https://forum.generic-mapping-tools.org/u/LIGAbaji)\
**Post date:** [July 15, 2022, 5:26pm UTC](https://forum.generic-mapping-tools.org/t/inconsistent-plot-between-julia-matrix-and-gmt-grid/2665/4 "2022-07-15T17:26:39Z")

</div>

@Cuda-Chen thanks for sharing this.
