Label pushed outside of canvas

I create a graph:

const double x[] = {0.0, 1.0};
const double y[] = {20000.0, 10000.0};
TGraph g(2, x, y);
g.GetYaxis() -> SetTitle("#frac{A}{B}");

When I draw it:

TCanvas c;
g.Draw("AP*");

the y axis label is pushed outside of the canvas:

How to fix this problem?

1 Like

It is because the axis labels are long and the title is automatically positioned to not overlap them. That was a recent request we had. Before the title would have overlapped the labels.

Do:

{
   const double x[] = {0.0, 1.0};  
   const double y[] = {20000.0, 10000.0};
   TGraph g(2, x, y);
   g.GetYaxis() -> SetTitle("#frac{A}{B}");
   g.GetYaxis() -> SetTitleOffset(1.);
   g.Draw("A*");
}

Now the result is:

Luckily the y axis title has not bumped into the axis label (20000).

If I set the y axis title as follows

g.GetYaxis() -> SetTitle("-2ln#frac{A}{B}");

the result is no longer acceptable:

Now I could ask the question again - how to fix the problem?

{
  const double x[] = {0.0, 1.0};
  const double y[] = {20000.0, 10000.0};
  TGraph g(2, x, y);
  g.GetYaxis()->SetTitle("-2ln#frac{A}{B}");
  g.GetYaxis()->SetTitleOffset(5.);
  g.GetYaxis()->CenterTitle(kTRUE);
  g.GetXaxis()->SetTitle("#frac{A}{B}");
  g.GetXaxis()->SetTitleOffset(5.);
  g.GetXaxis()->CenterTitle(kTRUE);
  TCanvas *c = new TCanvas("c", "c");
  if (gPad) gPad->SetLeftMargin(0.5);
  if (gPad) gPad->SetBottomMargin(0.5);
  g.Draw("A*");
}
{
   auto *c1 = new TCanvas();
   c1->SetLeftMargin(0.2);
   const double x[] = {0.0, 1.0};
   const double y[] = {20000.0, 10000.0};
   TGraph g(2, x, y);
   g.GetYaxis() -> SetTitle("-2ln#frac{A}{B}");
   g.Draw("A*");
}

this all seems a bit fragile.

a probably better strategy would be for the ROOT painter to automatically compute the width+height needed for the X/Y axes, their ticks+ticks’ labels, reserve enough space for those, and then display the axis-label in the leftover space.
no user intervention needed.

(that’s what we do in gonum/plot…)

1 Like

Yes that’s always the same discussion “automatic” vs “user control” …
If all is automatic there is no way to define precisely a plot ready for publication placing precisely all the elements.
We are now by default placing the Y axis title automatically and still there is cases like this one where adjustments are required. You suggest more automatic setting… thats all fine until somebody comes with a case where the automatic placements are not producing what he/she wants and we are back in the same situation …that’s a bit endless.

yes, nature or nurture :slight_smile:

still, there is a difference between having something automatic that automatically put overlaping glyphs (as is reported here) and something that doesn’t fit someone’s aesthetical taste.

1 Like

Another recent discussion about this:

1 Like

Besides

c1 -> SetLeftMargin(0.2);

one also needs to call

g.GetYaxis() -> SetTitleOffset(2.0);

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.