# TGraph title and axis problems

**URL:** <https://root-forum.cern.ch/t/tgraph-title-and-axis-problems/15241>\
**Category:** ROOT\
**Created:** [November 14, 2012, 3:37pm UTC](https://root-forum.cern.ch/t/tgraph-title-and-axis-problems/15241 "2012-11-14T15:37:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![William\_Heintzelman](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/c57346/32.png) [@William\_Heintzelman](https://root-forum.cern.ch/u/William_Heintzelman)\
**Post date:** [November 14, 2012, 3:37pm UTC](https://root-forum.cern.ch/t/tgraph-title-and-axis-problems/15241/1 "2012-11-14T15:37:27Z")

</div>

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](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](mailto: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](http://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] = i_0.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] = i_0.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] = i_0.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”);  
}

* * *

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [November 14, 2012, 4:20pm UTC](https://root-forum.cern.ch/t/tgraph-title-and-axis-problems/15241/2 "2012-11-14T16:20:44Z")

</div>

```cpp
{
  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
}[/code] [code]{
  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:  
[https://root-forum.cern.ch/t/order-of-calling-methods-with-tmultigraph/14724/2](https://root-forum.cern.ch/t/order-of-calling-methods-with-tmultigraph/14724/2)  
[https://root-forum.cern.ch/t/setting-titles-for-tgraphs/14856/1](https://root-forum.cern.ch/t/setting-titles-for-tgraphs/14856/1)  
[https://root-forum.cern.ch/t/setrangeuser-and-settitle-working-with-thstacks-and-th1f-obj/12114/1](https://root-forum.cern.ch/t/setrangeuser-and-settitle-working-with-thstacks-and-th1f-obj/12114/1)  
[https://root-forum.cern.ch/t/setaxisrange-0-0-1-0-y-is-ineffective/14040/1](https://root-forum.cern.ch/t/setaxisrange-0-0-1-0-y-is-ineffective/14040/1)  
[https://root-forum.cern.ch/t/palette-on-a-tprofile2d/14704/1](https://root-forum.cern.ch/t/palette-on-a-tprofile2d/14704/1)  
[https://root-forum.cern.ch/t/setrangeuser-doesnt-work-well/14774/1](https://root-forum.cern.ch/t/setrangeuser-doesnt-work-well/14774/1)  
[https://root-forum.cern.ch/t/drawing-a-tgraphasymmerrors/14205/1](https://root-forum.cern.ch/t/drawing-a-tgraphasymmerrors/14205/1)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 15, 2012, 9:59am UTC](https://root-forum.cern.ch/t/tgraph-title-and-axis-problems/15241/3 "2012-11-15T09:59:50Z")

</div>

> [@](#):
>
> 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?
> 
> * * *
> 
> Follow-up Comment:
> 
> do:
> 
> c1-\>DrawFrame(0,2,2.2,11)-\>GetXaxis()-\>SetTitle(“layer No”);;

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](http://root.cern.ch/root/html/TPad.html#TPad:DrawFrame)
