TGraph2D axes formatting


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


Hi Rooters,

I am creating multiple 3D graphs using the TGraph2D function. It’s going well; except, the SetNumberContours doesn’t get saved to the Macro. I created a subroutine that I have used successfully with 1 and 2 dimensional histograms and TGraph. However, this doesn’t work with TGraph2D regardless if I put the axes format commands in the main program or a subroutine. There seems to be nothing about formatting axes in the manual.

Any ideas?

Rob

#include <TROOT>
#include <TAxis>
#include <TStyle>
#include <TGraph2D>
#include <TCanvas>
#include <TString>
#include "TMath.h"
#include <stdio>
#include <stdlib>
#include <math>
#include <cmath>
#include <string>
#include <iostream>
#include <fstream>

int test()
{
        TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,400);
        Double_t x, y, z, P = 6.;
        Int_t np = 200;
        TGraph2D *dt = new TGraph2D();
        dt->SetTitle("Graph title; X axis title; Y axis title; Z axis title");
        TRandom *r = new TRandom();

        for (Int_t N=0; N<np; N++) {
                x = 2*P*(r->Rndm(N))-P;
                y = 2*P*(r->Rndm(N))-P;
                z = (sin(x)/x)*(sin(y)/y)+0.2;
                dt->SetPoint(N,x,y,z);
        }
        gStyle->SetPalette(1);
        gStyle->SetNumberContours(99);

        char *mainTitle = "Main Title";
        char *xTitle = "X Title";
        char *yTitle = "Y Title";
        char *zTitle = "Z Title";
        SetupAxesg( dt,mainTitle,xTitle,yTitle,zTitle);
        dt->Draw("surf1");
        return 0;
}
void SetupAxesg( TGraph2D *gr, char *mainTitle, char *xTitle, char *yTitle, char *zTitle )
{
printf("%s %s %s %s\n",mainTitle,xTitle,yTitle,zTitle);
        gr -> SetTitle(mainTitle);
        gr -> GetXaxis() -> SetTitle(xTitle);
        gr -> GetXaxis() -> SetTitleSize(0.04);
        gr -> GetXaxis() -> SetLabelSize(0.03);
        gr -> GetXaxis() -> SetDecimals(kTRUE);
        gr -> GetXaxis() -> CenterTitle(true);

        gr -> GetYaxis() -> SetTitle(yTitle);
        gr -> GetYaxis() -> SetDecimals(kTRUE);
        gr -> GetYaxis() -> SetTitleSize(0.04);
        gr -> GetYaxis() -> SetLabelSize(0.03);
        gr -> GetYaxis() -> CenterTitle(true);
        gr -> GetYaxis() -> SetTitleOffset(1.10);

        gr -> GetZaxis() -> SetTitle(zTitle);
        gr -> GetZaxis() -> SetDecimals(kTRUE);
        gr -> GetZaxis() -> SetTitleSize(0.04);
        gr -> GetZaxis() -> SetLabelSize(0.03);
        gr -> GetZaxis() -> CenterTitle(true);
        gr -> GetZaxis() -> SetTitleOffset(1.10);
}

The axis are hold by the underlying histogram created when the 2D graph is drawn.

do

        dt->Draw("surf1");
        gPad->Update();
        SetupAxesg( dt,mainTitle,xTitle,yTitle,zTitle);

Thanks, Olivier! That works!

Rob

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