I would suggest the attached code…
This code uses the Pad limits to define the axis length instead of the histogram min and max. Histogram min and max are always the same and when you zoom the length of the axis becomes very long compare to the window you see on screen. As the tick length and labels position are in % of the axis length they change accordingly … Then, when you zoom, the pad coordinates change, from the initial value, so the TGaxis needs to accommodate this changes in real time. That is why it I embedded it in a TExec,
TH1F *h;
TF1 *f;
void test()
{
// main frame
TGMainFrame *fMainFrame673 = new TGMainFrame(gClient->GetRoot(),10,10,kMainFrame | kVerticalFrame);
fMainFrame673->SetName("fMainFrame673");
fMainFrame673->SetLayoutBroken(kTRUE);
// embedded canvas
TRootEmbeddedCanvas *fRootEmbeddedCanvas560 = new TRootEmbeddedCanvas(0,fMainFrame673,360,288);
fRootEmbeddedCanvas560->SetName("fRootEmbeddedCanvas560");
Int_t wfRootEmbeddedCanvas560 = fRootEmbeddedCanvas560->GetCanvasWindowId();
TCanvas *c123 = new TCanvas("c123", 10, 10, wfRootEmbeddedCanvas560);
fRootEmbeddedCanvas560->AdoptCanvas(c123);
fMainFrame673->AddFrame(fRootEmbeddedCanvas560, new TGLayoutHints(kLHintsLeft | kLHintsTop,2,2,2,2));
fRootEmbeddedCanvas560->MoveResize(0,0,600,600);
h =new TH1F("h","h",100,0,100);
h->Draw();
f = new TF1("f","pol1",0,100);
f->SetParameters(0,.1);
TExec *ex = new TExec("ex","DrawAxisCal();");
ex->Draw();
fMainFrame673->SetMWMHints(kMWMDecorAll,
kMWMFuncAll,
kMWMInputModeless);
fMainFrame673->MapSubwindows();
fMainFrame673->Resize(fMainFrame673->GetDefaultSize());
fMainFrame673->MapWindow();
fMainFrame673->Resize(600,600);
}
void DrawAxisCal()
{
TGaxis *AxisCal = new TGaxis(
gPad->GetUxmin(),
gPad->PadtoY(0.01),
gPad->GetUxmax(),
gPad->PadtoY(0.01),
f->Eval(h->GetXaxis()->GetXmin()),
f->Eval(h->GetXaxis()->GetXmax()),
510,"S-");
AxisCal->SetTickSize(0.02);
AxisCal->Paint();
}