Change histogram's title on canvas

I am drawing a few histograms on the same canvas. By default, the title of the whole plot, is the name of the first histogram. What I want to do is change it to something different, for instance “Plot” instead of “Experimental” on the following image. I want to do that ONLY on the title and not on the legend.

I’ve thought of adding a TPaveText on top of the title but I can’t seem to be able to draw it. Is there to change the title of the plot without having to change the histogram’s name?

Thank’s in advance.

My code is

#include "Riostream.h"
void HistogramFromAscii(){//(char * file_c) {

   gROOT->SetStyle("Plain");
   gStyle->SetOptStat(0000);
   gStyle->SetOptFit(1111);
   
   TPaveText *t=new TPaveText(0.8,0.8,1,1,"brNDC");
   
   TCanvas *c = new TCanvas("c", "c");
   c->SetFillColor(5);
   c->SetFrameFillColor(10);
      
   TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
   dir.ReplaceAll("HistogramFromAscii.C","");
   dir.ReplaceAll("/./","/");
   ifstream in;
   in.open(Form("%sZnF2_2600_170_Jesus.dat",dir.Data()));

   Float_t x,y,z,w,k,l;
   Int_t nlines = 0;
   //TFile *f = new TFile("HistogramFromAscii.root","RECREATE");
   TH1F *h1 = new TH1F("h1","Experimental",1024,1,1024);
   TH1F *h2 = new TH1F("h2","Total SImulated",1024,1,1024);
   TH1F *h3 = new TH1F("h3","Fluorine, Jesus et al.",1024,1,1024);
   TH1F *h4 = new TH1F("h4","Zn, Rutherford",1024,1,1024);
   TH1F *h5 = new TH1F("h5","Au, Rutherford",1024,1,1024);
   //TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","x:y:z");

   while (1) {
      in >> x >> y >> z >> w >> k >> l;
      if (!in.good()) break;
      if (nlines < 5) printf("x=%8f, y=%8f\n",x,y);
      h1->Fill(x,y);
      h2->Fill(x,z);
      h3->Fill(x,w);
      h4->Fill(x,k);
      h5->Fill(x,l);
      //ntuple->Fill(x,y,z);
      nlines++;
   }
   printf("Found %d points\n",nlines);

   in.close();
   h1->SetLineColor(kRed);
   h1->SetLineWidth(4);
   h1->GetXaxis()->SetRangeUser(0,750);
   h1->Draw();
   h2->SetLineColor(kBlue);
   h2->SetLineWidth(2);
   h2->Draw("same");
   h3->SetLineColor(kMagenta+1);
   h3->SetLineWidth(2);
   h3->Draw("same");
   h4->SetLineColor(kGreen+3);
   h4->SetLineWidth(2);
   h4->Draw("same");
   h5->SetLineColor(kBlack);
   h5->SetLineWidth(2);
   h5->Draw("same");
   
   t->AddText("hola");
   t->Draw();
   
   TLegend *leg = c->BuildLegend();
   
   //return c;
   //leg->SetFillStyle(0);
   
   
   //c->SetLogy();   
   //c->SaveAs("alpha_in_Si.pdf");
   
}

Before drawing anything, use:
gStyle->SetOptTitle(0);
then something like:
TPaveText *t = new TPaveText(0.0, 0.9, 0.3, 1.0, “brNDC”); // left-up

1 Like

I used your code just like you said, but it didn’t work. Then I thought that you might have forgotten

t->Draw();

so I added it but again with no luck. My code is the following

  #include "Riostream.h"
  void HistogramFromAscii(){//(char * file_c) {

    gROOT->SetStyle("Plain");
    gStyle->SetOptStat(0000);
    gStyle->SetOptFit(1111);
    gStyle->SetOptTitle(0);
    TPaveText *t = new TPaveText(0.0, 0.9, 0.3, 1.0, "ljhg;kijhg;kihg;khg");
    //t->Draw();// left-up
    
    //TPaveText *t=new TPaveText(0.8,0.8,1,1,"brNDC");
    
    TCanvas *c = new TCanvas("c", "c");
    c->SetFillColor(5);
    c->SetFrameFillColor(10);
	
    TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
    dir.ReplaceAll("HistogramFromAscii.C","");
    dir.ReplaceAll("/./","/");
    ifstream in;
    in.open(Form("%sMgB2.dat",dir.Data()));

    Float_t x,y,z,w,k,l,m;
    Int_t nlines = 0;
    //TFile *f = new TFile("HistogramFromAscii.root","RECREATE");
    TH1F *h1 = new TH1F("h1","Experimental",1024,1,1024);
    TH1F *h2 = new TH1F("h2","Total Simulation",1024,1,1024);
    TH1F *h3 = new TH1F("h3","^{nat}B, Chiari M.",1024,1,1024);
    TH1F *h4 = new TH1F("h4","C, Sigma Calc.",1024,1,1024);
    TH1F *h5 = new TH1F("h5","O, Sigma Calc.",1024,1,1024);
    TH1F *h6 = new TH1F("h6","Mg, Sigma Calc.",1024,1,1024);
    
    //TNtuple *ntuple = new TNtuple("ntuple","data from ascii file","x:y:z");

    while (1) {
	in >> x >> y >> z >> w >> k >> l >> m;
	if (!in.good()) break;
	if (nlines < 5) printf("x=%8f, y=%8f, z=%8f, w=%8f, k=%8f, l=%8f, m=%8f\n",x,y,z,w,k,l,m);
	h1->Fill(x,y);
	h2->Fill(x,z);
	h3->Fill(x,w);
	h4->Fill(x,k);
	h5->Fill(x,l);
	h6->Fill(x,m);
	//ntuple->Fill(x,y,z);
	nlines++;
    }
    printf("Found %d points\n",nlines);

    in.close();
    h1->SetLineColor(kRed);
    h1->SetLineWidth(4);
    h1->GetXaxis()->SetRangeUser(0,750);
    h1->Draw();
    h2->SetLineColor(kBlue);
    h2->SetLineWidth(2);
    h2->Draw("same");
    h1->GetXaxis()->SetTitle("Channels");
    h1->GetYaxis()->SetTitle("Events");
    h3->SetLineColor(kMagenta+1);
    h3->SetLineWidth(2);
    h3->Draw("same");
    h4->SetLineColor(kGreen+3);
    h4->SetLineWidth(2);
    h4->Draw("same");
    h5->SetLineColor(kBlack);
    h5->SetLineWidth(2);
    h5->Draw("same");
    h6->SetLineColor(kCyan+1);
    h6->SetLineWidth(2);
    h6->Draw("same");    
    //t->AddText("hola");
    //t->Draw();    
    TLegend *leg = c->BuildLegend();
    t->Draw();
    
  }

The only difference is that there is no title on the plot due to OptTitle(0)

Your code cannot be run because of missing files. Here is a little example:

{
   TCanvas *c1 = new TCanvas("c1", "c1",1024,44,926,686);
   gStyle->SetOptTitle(0);

   TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);
   hpx->SetBinContent(0,1);
   hpx->SetBinContent(1,1);
   hpx->SetBinContent(2,1);
   hpx->SetBinContent(3,1);
   hpx->SetBinContent(4,1);
   hpx->SetBinContent(6,1);
   hpx->SetBinContent(7,2);
   hpx->SetBinContent(8,4);
   hpx->SetBinContent(9,4);
   hpx->SetBinContent(10,5);
   hpx->SetBinContent(11,2);
   hpx->SetBinContent(12,6);

   hpx->Draw("");

   pl = new TPaveLabel(-2.722343,6.44797,1.572668,6.937022,"New Title","br");
   pl->Draw();
}

If you set a strange “ljhg;kijhg;kihg;khg” option for the TPaveText, why are you surprised that it doesn’t work?
I guess it should work fine if you try:

TPaveText *t = new TPaveText(0.0, 0.9, 0.3, 1.0, "brNDC"); // left-up
t->AddText("hola");

Or you can use Olivier’s suggestion:

TPaveLabel *t = new TPaveLabel(0.0, 0.9, 0.3, 1.0, "hola", "brNDC"); // left-up

Note that I built that little code example using the toolbar in the canvas.
Then you save the canvas in a .C file an you get the corresponding code.
That’s a good way to learn how to use some of the graphic classes like TPaveText.

This really worked, but I should add

t->Draw();

How to make it look like it was the histogram’s name i.e. just a white box without background shade.

t->SetBorderSize(0);
t->SetFillColor(gStyle->GetTitleFillColor());

Draw a legend so that the text is fixed there.
Then change the title of the first histogram.
Delete the"title" TPaveText object from the list of primitives on the canvas.
Update the canvas. This will re-create the title.
Deleting the "title" object is necessary to get a new one with proper size.
Example in python:

c = ROOT.TCanvas()
l = c.GetListOfPrimitives()

# draw what you need to draw

c.BuildLegend().Draw()
c.Update()

l[1].SetTitle("New title") # l[0] is TFrame
l.Remove(l.FindObject("title"))
c.Update() # will recreate title
2 Likes