RootTalk


ROOT Discussion Forums

TGraph2D: logarithmic z-axis  SOLVED

Discuss installing and running ROOT here. Please post bug reports here.

Moderator: rootdev

TGraph2D: logarithmic z-axis

Postby jmikosch » Wed Aug 14, 2013 16:44

Hi,

I'd like to plot a TGraph2D with a logarithmic z-axis. TPad::SetLogz() will set the z-axis to log-scale and adjust the TPaletteAxis, but the color palette does not translate to the graph. It works for TH2F though. You can see this from the following example, which is derived from the standard example in the TGraph2D documentation.

Any idea what is going on here and how to fix it?

Thanks,
Jochen
Code: Select all
{
  //logarithmic z-axis - color palette:
  //it works for TH2F but not for TGraph2D

   TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,800);
   c->Divide(1,2); 
   c_1->SetLogz();
   c_2->SetLogz();

   Double_t x, y, z, P = 6.;

   Int_t np_g = 200;
   Int_t np_h = 20000;
   TGraph2D *dt = new TGraph2D();
   TH2F *h = new TH2F("TH2F","TH2F",50,-6,6,50,-6,6);
   TRandom *r = new TRandom();

   for (Int_t N=0; N<np_g; 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);
   }

   for (Int_t N=0; N<np_h; N++) {
      x = 2*P*(r->Rndm(N))-P;
      y = 2*P*(r->Rndm(N))-P;
      z = (sin(x)/x)*(sin(y)/y)+0.2;
      h->Fill(x,y,z);
   }

   gStyle->SetPalette(1);
   h->SetStats(0);
   //h->GetZaxis()->SetRangeUser(1e-3,5);
   //dt->GetZaxis()->SetRangeUser(1e-3,5);

   c->cd(1);
   dt->Draw("tri1z");
   c->cd(2);
   h->Draw("surf2z");
}
Attachments
TGraph2D_vs_TH2F.gif
jmikosch
 
Posts: 4
Joined: Wed Aug 14, 2013 2:39

Re: TGraph2D: logarithmic z-axis

Postby couet » Wed Aug 14, 2013 16:51

You should have an old version of ROOT. With 5.34 on Mac your macro gives me the attached picture.
Attachments
c.png
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN

Re: TGraph2D: logarithmic z-axis

Postby jmikosch » Wed Aug 14, 2013 17:05

Thanks, Olivier. I have version 5.26. Looks like time to upgrade...
jmikosch
 
Posts: 4
Joined: Wed Aug 14, 2013 2:39

Re: TGraph2D: logarithmic z-axis

Postby jmikosch » Thu Aug 15, 2013 5:09

Hi,

I have upgraded to the current ROOT version v5.34 as suggested. Unfortunately, I am running into a related problem now. The command TGraph2D::GetZaxis()->SetRangeUser(a,b) is ignored in the macro. Adjusting the axis "by hand" afterwards in the TPad works, but once again the color palette does not translate to the graph. Similarly, if I use TGraph2D::SetMinimum(a) and TGraph2D::SetMaximum(b) in the macro, the z-axis and the TPaletteAxis is adjusted, but it does not translate to the graph. See for macro and plot below.

The reason I want to do this is to have two TGraph2Ds back to back with the same z-axis range for better comparison.

Thanks,
Jochen

Code: Select all
{
  //logarithmic z-axis - color palette:
  //it works for TH2F but not for TGraph2D

   TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,800);
   c->Divide(1,2); 
   c_1->SetLogz();
   c_2->SetLogz();

   Double_t x, y, z, P = 6.;

   Int_t np_g = 200;
   Int_t np_h = 20000;
   TGraph2D *dt = new TGraph2D();
   TH2F *h = new TH2F("TH2F","TH2F",50,-6,6,50,-6,6);
   TRandom *r = new TRandom();

   for (Int_t N=0; N<np_g; 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);
   }

   for (Int_t N=0; N<np_h; N++) {
      x = 2*P*(r->Rndm(N))-P;
      y = 2*P*(r->Rndm(N))-P;
      z = (sin(x)/x)*(sin(y)/y)+0.2;
      h->Fill(x,y,z);
   }

   gStyle->SetPalette(1);
   h->SetStats(0);
   h->GetZaxis()->SetRangeUser(1e-3,1e3);
   //dt->GetZaxis()->SetRangeUser(1e-3,1e3); //this command is ignored
   dt->SetMaximum(1e3);
   dt->SetMinimum(1e-3);

   c->cd(1);
   dt->Draw("tri1z");
   c->cd(2);
   h->Draw("surf2z");
}
Attachments
TGraph2D_vs_TH2F_1.gif
jmikosch
 
Posts: 4
Joined: Wed Aug 14, 2013 2:39

Re: TGraph2D: logarithmic z-axis

Postby couet » Thu Aug 15, 2013 8:29

I see what you mean. The TGraph2D plot uses the whole palette unlike the TH2 which use the correct range of colors. I am investigating.
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN

Re: TGraph2D: logarithmic z-axis  SOLVED

Postby couet » Thu Aug 15, 2013 12:15

This problem is now fixed in 534 and in the trunk.
With your macro, I now get the attached picture.
Attachments
c.png
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN

Re: TGraph2D: logarithmic z-axis

Postby jmikosch » Fri Aug 16, 2013 17:26

Thanks a lot Olivier for the excellent support! I am very impressed with the promptness of your replies and the fix. I managed to get my plot done with the trunk version obtained from git.

Jochen
jmikosch
 
Posts: 4
Joined: Wed Aug 14, 2013 2:39

Re: TGraph2D: logarithmic z-axis

Postby couet » Mon Aug 19, 2013 8:03

That's just my job :-)
Thanks :)
User avatar
couet
 
Posts: 5511
Joined: Tue Sep 02, 2003 8:32
Location: CERN


Return to ROOT Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 5 guests