Multiple TGraph2D with option "PCOL" share the same palette

Dear experts,

I would like to draw multiple TGraph2D with option "PCOL" and make them share the same palette. Furthermore, I would like to have colors of markers changed when the maximum/minimum of the palette axis are modified.

I noticed a post here. But I do not think the colors of TGraph2Ds follow palette axis of TH3F. I tried to draw the palette axis using “BOX2 Z” option and changed the maximum and minimum of TH3F. TGraph2D color is out of sync with TH3F color bar. How can I get the color palette properly shown?

Thanks in advance!

Please read tips for efficient and successful posting and posting code

ROOT Version: 6.28
Platform: Not Provided
Compiler: gcc 12


Thanks for the post. While I add the graphics expert, @couet, in the loop, could you perhaps share a minimal reproducer of what you are trying to achieve? Maybe with only 2 graphs?


The post you mentioned is the way to proceed. The TH3F has nothing to do with the palette it is only use to draw the axis.
Also note that a TGraph2D has only 3 variables therefore the color is mapped on the 3rd one. There is no additional information given by the color.

I have to set maximum and minimum of each TGraph2D with the same values, in order to draw them with proper color under the range which color bar represents.

In order to change the axis ranges, I have to modify the maximum and minimum of TGraph2D at first. Otherwise, TGraph2D::GetXaxis()->SetLimits() will not work and root will automatically decide the ranges.

A minimal example is

void testgraph2d()
  TGraph2D* g1 = new TGraph2D(3);
  g1->SetPoint(0, 1, 1, 1);
  g1->SetPoint(1, 1, 2, 2);
  g1->SetPoint(2, 2, 1, 3);
  // must be called at first
  g1->GetXaxis()->SetLimits(0, 4);
  g1->GetYaxis()->SetLimits(0, 4);
  g1->Draw("PCOL Z");

  TGraph2D* g2 = new TGraph2D(3);
  g2->SetPoint(0, 3, 2, 7);
  g2->SetPoint(1, 2, 3, 8);
  g2->SetPoint(2, 3, 3, 9);
  g2->Draw("PCOL SAME");