How to plot values from an array as a 3D surface


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Dear ROOT users,

I have a data set which represents elevation above sea level at different latitudes and longitudes. This data is in an array in the following form:

           -90.0     -89.9833     -89.96667     -89.96 ...
 -180 

-179.9833

-179.9667

-179.95
.
.

where the array index represents latitude and longitude horizontally and vertically, respectively. The elements contain the elevation above sea level, which is of type double.

I’m unsure of a way in which I can store this data so that I can plot a 3D surface in ROOT. If anyone could guide me I’d appreciate it.

Please ignore this post.

You can do this with a TH2D:
Assuming you have a 2D Array which stores the sea levels and 2 1DArrays which store the latitude and longitude corresponding to the indices and the Arrays are of length M, N and MxN, you can use the following code:

Double_t longitude[M];
Double_t latitude[N];
Double_t sealevel[M][N];

// Fill the arrays here

TH2D* hSeaLevel = new TH2D("hSeaLevel", "Sea Level", M, longitude, N, latitude);

for (Int_t i = 1; i <= M; i++)
    for (Int_t j = 1; j <= N; j++)
        hSeaLevel->SetBinContent(i, j, sealevel[i-1][j-1]);

hSeaLevel->Draw("SURF");

The SURF draw option is just one possibility. There are some more which give you similar results. A complete list including example plots can be found here:
https://root.cern/doc/v610/classTHistPainter.html#HP01c

Hi,
i think a simpler way is using a TGraph2D:
https://root.cern.ch/doc/v614/classTGraph2D.html
In this case one doesnt need to care about binnings, empty bins etc.
Painting methods are essentially same as for TH2.

Btw: In the case of TH2D with variable bin size as proposed the x or y arrays
are the bin edges so one needs to provide Nbins+1 values, see:
https://root.cern.ch/root/html534/guides/users-guide/Histograms.html#Fixed.or.Variable.Bin.Width

Cheers
Otto

1 Like

Of course @OSchaile is right, it’s much easier with a TGraph2D, I should not post in the morning without coffee. XD

I’ve rewritten you the example:

Double_t longitude[M];
Double_t latitude[N];
Double_t sealevel[M][N];

// Fill the arrays here

TGraph2D* tgSeaLevel = new TGraph2D(M*N);

for (Int_t i = 0; i < M; i++)
    for (Int_t j = 0; j < N; j++)
        tgSeaLevel->SetPoint(i*N+j, longitude[i], latitude[j], sealevel[i][j]);

tgSeaLevel->Draw("SURF");

You can find further information here:
https://root.cern.ch/doc/master/classTGraph2D.html

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.