TGraph2D: access xyz range and title, out of sync color bar

Hi everybody,

I have had quite a lot of pain to get TGraph2D producing the graph I had in mind.
But there are a few open questions… I’d be very thankful for pointing out mistakes or giving suggestions .

What I wanted to do is to generate a 2D color filled contour plot x*y where z is color axis.
The data constists of not-equidistant datapoints (the example code uses artificial data).
This worked quite well by using TGraph2d, the drawing option TRI2Z and rotating the canvas for the impression of a 2D graph.

problem 1:
I was not able to access the axis ranges or titles:

[code]#include <TCanvas.h>
#include <TAxis.h>
#include <TGraph2D.h>
#include <TH2.h>

int TGraph2D_example(){

double x[6] = {0,0.5,2,0,1,2.5};
double y[6] = {0,1.0,0.5,1,3,4};
double z[6] = {1,2,3,4,5,6};

TGraph2D *g = new TGraph2D();

for (int i = 0; i<6; i++) {
g->SetPoint(i,x[i],y[i],z[i]);
}

TCanvas *c1 = new TCanvas(“c1”);
c1->cd();

g->Draw(“TRI2 Z”);
g->GetXaxis()->SetTitle(“U / kV”);
g->GetYaxis()->SetTitle(“I / #muA”);
g->GetZaxis()->SetTitle("#delta / 10^{-3}");

c1->SetTheta(90.0-0.001);
c1->SetPhi(0.0+0.001);
c1->SetRightMargin(0.17);
c1->SetLeftMargin(0.125);

c1->Modified();
c1->Update();
c1->Print(“TGraph2D_example1.png”);

return(0);
}[/code]

gave the following result:


The strange thing here is, that pasting the same code in the interactive console (not compiling it…) gave the result I wanted (more or less).


I tried to plot a defined frame and then overplot the TGraph2D with the SAME option.
This kind of worked, but I ended up being able to access XY axis but not Z.

Then I found the SetHistogram() option…
I am not sure why I was able the access the axis by this trick.

[code]#include <TCanvas.h>
#include <TAxis.h>
#include <TGraph2D.h>
#include <TH2.h>

int TGraph2D_example(){

double x[6] = {0,0.5,2,0,1,2.5};
double y[6] = {0,1.0,0.5,1,3,4};
double z[6] = {1,2,3,4,5,6};

TGraph2D *g = new TGraph2D();
TH2D *h = new TH2D(“h”,"",100,0,3,100,0,4);

for (int i = 0; i<6; i++) {
g->SetPoint(i,x[i],y[i],z[i]);
}

TCanvas *c1 = new TCanvas(“c1”);
c1->cd();

g->SetHistogram(h);
g->Draw(“TRI2 Z”);
g->GetXaxis()->SetTitle(“U / kV”);
g->GetYaxis()->SetTitle(“I / #muA”);
g->GetZaxis()->SetTitle("#delta / 10^{-3}");

c1->SetTheta(90.0-0.001);
c1->SetPhi(0.0+0.001);
c1->SetRightMargin(0.17);
c1->SetLeftMargin(0.125);

c1->Modified();
c1->Update();
c1->Print(“TGraph2D_example.pdf”);

return(0);
}[/code]

Now the graph looked like this:


problem 2:
The next thing I encountered was the color bar on the right being out of sync with the colors used for the left plot. This can be visualized by rotating the canvas:


So what I did was setting minimum and maximum manually.

[code]#include <TCanvas.h>
#include <TAxis.h>
#include <TGraph2D.h>
#include <TH2.h>

int TGraph2D_example(){

double x[6] = {0,0.5,2,0,1,2.5};
double y[6] = {0,1.0,0.5,1,3,4};
double z[6] = {1,2,3,4,5,6};

TGraph2D *g = new TGraph2D();
TH2D *h = new TH2D(“h”,"",100,0,3,100,0,4);

for (int i = 0; i<6; i++) {
g->SetPoint(i,x[i],y[i],z[i]);
}

TCanvas *c1 = new TCanvas(“c1”);
c1->cd();

g->SetHistogram(h);
g->Draw(“TRI2 Z”);
g->GetXaxis()->SetTitle(“U / kV”);
g->GetYaxis()->SetTitle(“I / #muA”);
g->GetZaxis()->SetTitle("#delta / 10^{-3}");
g->SetMinimum(g->GetZmin());
g->SetMaximum(g->GetZmax());

c1->SetTheta(90.0-0.001-90);
c1->SetPhi(0.0+0.001);
c1->SetRightMargin(0.17);
c1->SetLeftMargin(0.125);

c1->Modified();
c1->Update();
c1->Print(“TGraph2D_example5.png”);

return(0);
}[/code]


Is behavior generated intentionally?

Thanks in advance for any help or hints,

Markus

I am using ROOT 5.34/03 (branches/v5-34-00-patches@46829, Nov 25 2012, 12:10:00 on linuxx8664gcc)
I tested it with the SVN version, which seemed to have no effect.

2 Likes

so it seems you solved all you issues ? right ?

also note: root.cern.ch/drupal/content/rainbow-color-map

Kind of - but more or less quick and dirty.
A bad feeling remains of having done something wrong or unnecessarily complicated …

The open questions are:

1.) Why am I not able to access the axis of a TGraph2D for putting an axis title there?
2.) Why does it work when pasting the code in the console window?
3.) Why are the color colors used to draw and the color bar out of sync? (Is is connected to sethistogram ?)

Thanks for the colortable suggestion

The following version answers your questions except the Z title which does not appears on the palette and I shoudl check that.

{
   double x[6] = {0,0.5,2,0,1,2.5};
   double y[6] = {0,1.0,0.5,1,3,4};
   double z[6] = {1,2,3,4,5,6};

   TGraph2D *g = new TGraph2D();

   for (int i = 0; i<6; i++)   g->SetPoint(i,x[i],y[i],z[i]);

   TCanvas *c1 = new TCanvas("c1");

   g->Draw("TRI2 Z");
   gPad->Update();
   g->GetXaxis()->SetTitle("U / kV");
   g->GetYaxis()->SetTitle("I / #muA");
   g->GetZaxis()->SetTitle("#delta / 10^{-3}");

   c1->SetTheta(0.0-0.001);
   c1->SetPhi(0.0+0.001);
   gStyle->SetHistTopMargin(0);

  c1->Print("TGraph2D_example1.png");
}

Thank you for your reply… it solves the first problem :smiley:

But the color bar on the right side, does not give the right color code that was used to draw the plot.

The minimum in Z is 1 and the plot is generated with purple as the last color to identify the minimum… thats what I wanted
The color bar drawn identifies purple with 0 and connects blue with 1… so color bar drawn does not match the color in plot

Here is the plot I get running your code


The palette is in fact painted according to the histogram generated. That why it start at O.
(the palette is painted in THistpainter).

Like in : g->Draw(“col Z”);

you can do:

g->GetHistogram()->SetMinimum(1.);

Is there a way of separating the axis title and values so they don’t overlap? See attachment:

unnamed.pdf (87.8 KB)
I’m using TGraph2D and the Draw(“surf1”) method.

hpxpy->GetXaxis()->SetTitleOffset(2.);

Side Note: Your question is completely unrelated with the original post. Please avoid random posting like that. It makes the thread very confusing to follow and to use as reference. You should have created a new post.