{
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:
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();
}
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
}
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).
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.
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();
}