Inconsistent plot between Julia matrix and GMT grid

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
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)

  • GMT grid
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)

I also provide the input file (pdf_out.txt) for reproducing this issue:
pdf_out.txt (136.8 KB)

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)

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)

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
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)

  • grdimage
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)

At last, thanks for your help!

1 Like

@Cuda-Chen thanks for sharing this.