TGraph title and axis problems

I submitted the bug report shown below and got the response shown. But are there any plans to fix the design and/or coding flaws that cause the scripts such as the ones I submitted to not work as a user would reasonably expect them to?


Posted by: Olivier Couet
Related to: [ROOT bugs #98782] Problems with titles and axis minimum and
maximum in TGraph
URL: http://savannah.cern.ch/bugs/?98782

Follow-up Comment:

do:

c1->DrawFrame(0,2,2.2,11)->GetXaxis()->SetTitle(“layer No”);;

Status: None -> Clarified
Open/Closed: Open -> Closed

Submitted by:
Originator Email: billh@hep.upenn.edu
Bug / Feature: Bug report
Category: Graphics 2D/3D
Priority: 5 - Normal
Severity: 3 - Normal
Status: Clarified
Privacy: Public
Assigned to: couet
Open/Closed: Closed
Release: 5.32/00
Discussion Lock:
Operating System: GNU/Linux

-----Original Message-----
I would like to be able to draw multiple graphs on the same Pad with titles
and axis limits of my own choosing. In researching this, I have encountered
numerous difficulties. I am running ROOT 5.32/00. These look like bugs to me
but perhaps I am doing something wrong?

On the roottalk message board
(root.cern.ch/root/roottalk/roottalk03/5140.html) the following script
is shown as an example of how one can add a title to the x-axis of a TGraph,
with the axis limits set by drawing a frame first. But when I run it, the
title does not appear on the graph.

{
c1 = new TCanvas(“c1”,“A Simple Graph Example”,200,10,700,500);
c1->DrawFrame(0,2,2.2,11);
const Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i0.1+0.05;
y[i] = 10
sin(x[i]+0.2);
}
gr = new TGraph(n,x,y);
gr->SetMarkerStyle(21);
gr->Draw(“P”);

c1->Update();
gr->GetHistogram()->GetXaxis()->SetTitle(“layer No”);
c1->Modified();
}


I have tried using a TMultiGraph without creating a frame explicitly, and
then setting the x and y titles and axis limits. With this approach, the
titles do appear on the canvas and the x-axis limits are adjusted, but the
y-axis limits specified are ignored. (One must also to be sure not to
attempt to adjust the axes before the graph has been drawn. In that case the
histogram associated with the graph has not been created and the
mg->SetXaxis() function call causes the program to crash.)

{
const Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i0.1+0.05;
y[i] = 10
sin(x[i]+0.2);
}
TGraph* gr = new TGraph(n,x,y);
TMultiGraph* mg = new TMultiGraph();
mg->SetTitle(“title;xaxis title; yaxis title”);
mg->Add(gr,"");
TCanvas
c1 = new TCanvas();
mg->Draw(“a”);
mg->GetXaxis()->SetLimits(0.,3.);
mg->GetYaxis()->SetLimits(1.,12.);
c1->Modified();
c1->Update();
}


If with the TMultiGraph one instead attempts to draw a frame with the desired
limits before drawing the multigraph, the axis titles appear, but two sets of
superimposed numeric labels are drawn and the limits given in the DrawFrame
command are ignored in plotting the graph.

{
const Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i0.1+0.05;
y[i] = 10
sin(x[i]+0.2);
}
TGraph* gr = new TGraph(n,x,y);
TMultiGraph* mg = new TMultiGraph();
mg->SetTitle(“title;xaxis title; yaxis title”);
mg->Add(gr,"");
TCanvas
c1 = new TCanvas();
c1->DrawFrame(0.,1.,3.,12.);
mg->Draw(“a”);
}


{ const Int_t n = 20; Double_t x[n], y[n]; for (Int_t i = 0; i < n; i++) { x[i] = i * 0.1 + 0.05; y[i] = 10.0 * sin(x[i] + 0.2); } TGraph *gr = new TGraph(n, x, y); TCanvas *c1 = new TCanvas("c1", "c1"); TH1F *hf = gPad->DrawFrame(0., 1., 3., 12., // xmin, ymin, xmax, ymax "global title;X title;Y title;Z title"); gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn #if 0 /* 0 or 1 */ hf = ((TH1F *)(gPad->FindObject("hframe"))); #endif /* 0 or 1 */ // hf->SetTitle("novel global title;novel X title;novel Y title;novel Z title"); gr->Draw("*"); // hf->SetTitle("new global title;new X title;new Y title;new Z title"); gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn } { const Int_t n = 20; Double_t x[n], y[n]; for (Int_t i = 0; i < n; i++) { x[i] = i * 0.1 + 0.05; y[i] = 10.0 * sin(x[i] + 0.2); } TGraph *gr = new TGraph(n, x, y); TMultiGraph *mg = new TMultiGraph("mg", "global title;X title;Y title;Z title"); mg->Add(gr, "*"); TCanvas *c1 = new TCanvas("c1", "c1"); // mg->SetTitle("novel global title;novel X title;novel Y title;novel Z title"); mg->Draw("A"); gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn // mg->GetHistogram()->SetTitle("new title;new X title;new Y title;new Z title"); #if 1 /* 0 or 1 */ mg->GetXaxis()->SetLimits(0., 3.); // xmin, xmax mg->GetHistogram()->SetMinimum(1.); // ymin mg->GetHistogram()->SetMaximum(12.); // ymax #else /* 0 or 1 */ mg->GetXaxis()->SetLimits(0., 3.); // xmin, xmax mg->GetYaxis()->SetRangeUser(1., 12.); // ymin, ymax // mg->GetHistogram()->SetAxisRange(1., 12., "Y"); // ymin, ymax #endif /* 0 or 1 */ gPad->Modified(); gPad->Update(); // make sure it's really (re)drawn }
See also:
[url]Order of calling methods with TMultiGraph
[url]Setting titles for TGraphs
[url]SetRangeUser and SetTitle working with THStacks and TH1F obj
[url]SetAxisRange(0.0,1.0,“Y”); is ineffective
[url]Palette on a TProfile2d
[url]SetRangeUser doesn’t work well
[url]Drawing a TGraphAsymmErrors

There is nothing to fix. When you do DrawFrame() the axis belong to the Frame you just drew. DrawFrame() returns a TH1F. You should change the axis title on this TH1F. That is what my answer meant. I agree may be a bit too compact way to do it. You can get a pointer to the TH1F returned by DrawFrame and then use it to change the titles.
See root.cern.ch/root/html/TPad.html#TPad:DrawFrame