Extending Canvas When Object Is Outside Boundaries, and X-Axis Covering Box

I first draw a function, and then I draw an object that extends beyond the boundaries of the canvas.
Here is a basic example:

TF1 *f1 = new TF1(“f1”,“x”,0,10);
f1->Draw();
TBox *aBox = new TBox(5,0,15,20);
aBox->Draw();

The output looks like this (if using a green box):

Is there some way to extend the saved image file so that it encloses the entire green box (up to y=20 and x=15) as well?

Additionally, is there were some way to prevent the green box from covering the x-axis?

{
  TCanvas *c = new TCanvas("c", "My Canvas Title");
  c->DrawFrame(0., 0., 15., 20., "My Plot Title; My X Axis;My Y Axis");
  TF1 *f1 = new TF1("f1","x",0,10);
  f1->Draw("SAME");
  TBox *aBox = new TBox(5,0,15,20);
  aBox->Draw();
  gPad->RedrawAxis();
}

Thanks for the help!
I actually want the box to remain only partially enclosed by the frame, so that it can escape the graph axes. Can that be done? Here is an illustration of what I mean:

Edit: I am also interested in knowing whether the ticks can be resized/removed/decreased in quantity after gPad->RedrawAxis() is called.

Just define the box position accordingly.

The attributes of the of the redrawn should be the same of the original axis. If you change the size of the tsk marks on the original plot the redrawn axis should have the same ticks size.

Hi Couet, thanks for stopping by!
Could you please elaborate on this?

Edit:
Below is my attempt to redraw a modified x-axis tick over the box.

TCanvas *c = new TCanvas("c", "My Canvas Title");
c->DrawFrame(0., 0., 15., 20., "My Plot Title; My X Axis;My Y Axis"); 
TF1 *f1 = new TF1("f1","x",0,10);
f1 -> GetXaxis() ->SetTickLength(0.5);
f1 -> Draw();
TBox *aBox = new TBox(5,0,15,20);
aBox->Draw();
gPad->RedrawAxis();

The x-axis ticks do not redraw themselves, though. What am I missing here?

{
   TCanvas *c = new TCanvas("c", "My Canvas Title");
   c->DrawFrame(0., 0., 15., 20., "My Plot Title; My X Axis;My Y Axis");
   TF1 *f1 = new TF1("f1","x",0,10);
   f1->Draw("SAME");
   TBox *aBox = new TBox(5,0,15,20);
   aBox->Draw();
   gPad->RedrawAxis();
}

{
  TCanvas *c = new TCanvas("c", "My Canvas Title");
  TH1F *h = c->DrawFrame(0., 0., 15., 20., "My Plot Title; My X Axis;My Y Axis");
  h->GetXaxis()->SetTickLength(0.5);
  TF1 *f1 = new TF1("f1","x",0,10);
  f1->Draw("SAME");
  TBox *aBox = new TBox(5,0,15,20);
  aBox->Draw();
  gPad->RedrawAxis();
}

Yes Sorry i forgot about the TickLenght. It should be applied of the frame as Wile pointed. It gives this picture:

It looks like a bit like a grid … but not very nice. If you want a grid on the X axis you can do.

{
   TCanvas *c = new TCanvas("c", "My Canvas Title");
   c->SetGridx();
   c->DrawFrame(0., 0., 15., 20., "My Plot Title; My X Axis;My Y Axis");
   TF1 *f1 = new TF1("f1","x",0,10);
   f1->Draw("SAME");
   TBox *aBox = new TBox(5,0,15,20);
   aBox->Draw();
   gPad->RedrawAxis();    // Redraw the tick
   gPad->RedrawAxis("G"); // Redraw the Grid
}

and you will get:

Okay, that was immensely helpful!
My final issue is that graph axes should not stretch to include the entire box, but the box should still be shown in its entirety. Only part of the box will overlap the graph, but the image file should display both the graph and the box. How can this be done?

{
  TCanvas *c = new TCanvas("c", "My Canvas Title");
  TH1F *h =
    c->DrawFrame(0., 0., 15., 20., "My Plot Title; My X Axis;My Y Axis");
  h->GetXaxis()->SetTickLength(0.5);
  TBox *aBox = new TBox(5,0,15,20);
  aBox->Draw();
  TF1 *f1 = new TF1("f1","x",0,10);
  f1->Draw("SAME");
  gPad->RedrawAxis();
}

Sorry, I think there was a misunderstanding.
Say I would like to draw

 TBox *aBox = new TBox(10,15,3000,5000);

and

TH1F *h = c->DrawFrame(0., 0., 10., 15., "My Plot Title; My X Axis;My Y Axis");

That is, I do not want the axis frame to surround the box.
Then how can I save a picture such that both the box and the graph are shown? The picture should include all the pixels out to (3000,5000), but the graph axes should only extend to (10,15).

Thank you both for your patience.

It seems to me that you want to divide your canvas into several independent pads:

${ROOTSYS}/tutorials/graphics/canvas.C
${ROOTSYS}/tutorials/hist/logscales.C
${ROOTSYS}/tutorials/tree/ntuple1.C

may be this ?

Thanks once again.
I found the following solution to the box problem:
By resizing the top and right margins (using for example “myPad.SetTopMargin(0.3)”) one can move the axes away from their respective sides of the picture.
With the right margin values, the green/grey box’s top right corner will meet the top right corner of the whole picture.
If the box is very wide or tall, the canvas size can be changed to accommodate the new aspect ratio.

Edit: Both of the problems discussed in this thread are now completely solved, and their answers are documented in this thread.
Solutions:
Box overlapping axis
Including box in picture, but outside graph

Another option is to resize the box to the current axis limits, this doesn’t help if you start changing the ranges or zooming.

{
  TCanvas *c = new TCanvas("c", "My Canvas Title");
  TH1F *h =
    c->DrawFrame(0., 0., 15., 20., "My Plot Title; My X Axis;My Y Axis");
  h->GetXaxis()->SetTickLength(0.5);
  TBox *aBox = new TBox(5,0,3000,5000);
  TF1 *f1 = new TF1("f1","x",0,10);
  f1->Draw("SAME");
  aBox->Draw();
  if (aBox->GetX1() < gPad->GetUxmin()) aBox->SetX1(gPad->GetUxmin());
  if (aBox->GetX2() > gPad->GetUxmax()) aBox->SetX2(gPad->GetUxmax());
  if (aBox->GetY1() < gPad->GetUymin()) aBox->SetY1(gPad->GetUymin());
  if (aBox->GetY2() > gPad->GetUymax()) aBox->SetY2(gPad->GetUymax());
  gPad->RedrawAxis();
} 

Actually, using a TCutG as your box lets you arbitrarily set the view range and still have it inside the frame.

{ 
  TCanvas *c = new TCanvas("c", "My Canvas Title");
  TH1F *h =
    c->DrawFrame(0., 0., 15., 20., "My Plot Title; My X Axis;My Y Axis");
  h->GetXaxis()->SetTickLength(0.5);

  TCutG *aBoxLikeCut = new TCutG();
  aBoxLikeCut->SetPoint(0,5,0);
  aBoxLikeCut->SetPoint(1,5,3000);
  aBoxLikeCut->SetPoint(2,5000,3000);
  aBoxLikeCut->SetPoint(3,5000,0);
  aBoxLikeCut->SetPoint(4,5,0);
  aBoxLikeCut->SetFillColor(kGreen);
  
  TF1 *f1 = new TF1("f1","x",0,10);
  f1->Draw("SAME");
  aBoxLikeCut->Draw("F");
  gPad->RedrawAxis();
}

This can even be edited to remove the frame now:

{
  TCutG *aBoxLikeCut = new TCutG();
  aBoxLikeCut->SetPoint(0,5,0);
  aBoxLikeCut->SetPoint(1,5,3000);
  aBoxLikeCut->SetPoint(2,5000,3000);
  aBoxLikeCut->SetPoint(3,5000,0);
  aBoxLikeCut->SetPoint(4,5,0);
  aBoxLikeCut->SetFillColor(kGreen);

  TF1 *f1 = new TF1("f1","x",0,10);
  f1->GetHistogram()->GetXaxis()->SetTickLength(0.5);

  f1->Draw("");
  aBoxLikeCut->Draw("F SAME");
} 

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