Retrieve TPaletteAxis from TGraph2D

Hi everyone,
I’m currently stuck with a odd problem: I’m drawing a TGraph2D object with the “CONTZ” option (that is, contour plot with projection/colour palette for Z-axis). I would like to set a title for the palette axis, but it seems that:

  1. the GUI editor doesn’t handle this option (it provides however SetTitleSize() and SetTitleOffset(), which aren’t very useful since I’m not able to set a title for the object);
  2. I’m not able to retrieve the “palette” object via code since it is not bound to the TGraph2D object, but instead to the “transition histogram” which is created to handle with the “Z” option (as stated here);
  3. there is a method to obtain the TPaletteAxis pointer (shown here), but it works for histograms only.

Is there a way to get the pointer to the palette object OR to the temporary histogram used to draw the plot?
I’m using ROOT 5.34.18 in compiled mode (that is, by building my own c++ program with includes and so on).

Thanks in advance,

Andrea Demetrio

void graph2dp()
{
   TCanvas *c = new TCanvas("c","Graph3D example",0,0,700,600);

   Double_t P = 6.;
   Int_t np   = 500;    // generate this many nodes

   Double_t *rx=0, *ry=0, *rz=0;
   rx = new Double_t[np];
   ry = new Double_t[np];
   rz = new Double_t[np];

   TRandom *r = new TRandom();
   
   for (Int_t N=0; N<np; N++) {
      rx[N]=2*P*(r->Rndm(N))-P;
      ry[N]=2*P*(r->Rndm(N))-P;
      rz[N]=(sin(rx[N])/rx[N])*(sin(ry[N])/ry[N])+0.2;
   }
   TGraph2D *dt = new TGraph2D(np, rx, ry, rz);
   dt->Draw("cont4z");

   TH2D *h2 = dt->GetHistogram();
   h2->GetXaxis()->SetTitle("X Title");
   h2->GetYaxis()->SetTitle("Y Title");
   h2->GetZaxis()->SetTitle("Z Title");
}

Thanks a bunch,
That code isn’t exactly the solution to my enquiry, but showed me how to get the histogram and then manage the palette from there.
Here’s the code snippet which does what I needed to:

TGraph2D gr; //Filling the TGraph2D... [...] //Drawing TGraph2D TCanvas c; gr.Draw("CONTZ"); //refresh gPad as required to obtain the palette (see ROOT reference::TPaletteAxis) gPad->Update(); //Getting the palette from the TGraph2D ((TPaletteAxis*)gr.GetHistogram()->GetListOfFunctions()->FindObject("palette"))->GetAxis()->SetTitle("Title");
or, if you want to store the TPaletteAxis in a variable:

//Getting the palette from the TGraph2D TPaletteAxis *palette = (TPaletteAxis*)gr.GetHistogram()->GetListOfFunctions()->FindObject("palette"); palette->GetAxis()->SetTitle("Title");

I’ll tick this question as solved;)

Andrea Demetrio

I gathered that was you were missing :slight_smile: