The correct way to modify TPaveText's NDC

Let say I created a canvas, and I would like to modify the NDC of the title for the histogram.
the following code did not work and I got AttributeError: 'TObject' object has no attribute 'SetX1NDC'.
Using ROOT.gPad.Update() or Modified() also did not work.
please help

c = r.TCanvas("c","title",600,600)

histogram.Draw()

tpave = c.GetPrimitive('title')
tpave.SetX1NDC(0.3)
tpave.SetX2NDC(0.6)

c.Draw()

ROOT Version: 6.26.00 (conda)
Platform: ubuntu20.04
Compiler: gcc9


Try

import ROOT as r
r.gStyle.SetTitleX(0.2)
r.gStyle.SetTitleY(0.99)
c = r.TCanvas("c","title",600,600)
#...

It worked, but it is a ‘global’ way.
Do you know any ‘local’ way ?

I’m not sure there’s an option in NDC, but in any case I would remove the automatic titles with SetOptTitle(0) and draw my own titles at any position with TText or TLatex, which give more control anyway.

OK, so it’s not as straightforward as what I think how it should be…
thanks.

You could do something like this too:

{
  TCanvas *c=new TCanvas();
  TH1F *h1 = new TH1F("h1", "histo from a gaussian", 100, -3, 3);
  h1->FillRandom("gaus", 500);
  h1->Draw();
  c->Modified();
  c->Update();
  TPaveText *pt = (TPaveText*)(c->GetPrimitive("title"));
  pt->SetX1NDC(0.1);
  pt->SetX2NDC(0.5);
  pt->SetY1NDC(0.8);
  pt->SetY2NDC(0.9);
  c->Modified();
  c->Update();
}

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