Changing Axis Titles on THStack

Hi Folks,

I’m trying to draw a THStack with one set of axis titles, then modify the content histograms and draw the stack again with different axis titles.

However whilst preparing code for this I ran into the following problem: Having set the axis titles of the THStack when constructing it, I’m then unable to edit the titles. Here’s a little test macro to demonstrate the problem:


#include "TCanvas.h"
#include "THStack.h"
#include "TH1D.h"
#include "TRandom.h"

void testplot(){

TH1D * h = new TH1D("h","",10,-2,2);

TRandom *r = new TRandom();

double x;


for(int i=0;i<10000;i++){
x = r->Gaus(0,1);

h->Fill(x);

}


TCanvas *c = new TCanvas("c","c");

THStack *hs = new THStack("hs","Title ;X axis; Y axis");
hs->Add(h);

hs->Draw("HIST");


//no combination of these will change the Y axis title

hs->GetHistogram()->GetYaxis()->SetTitle("New Title");
//hs->GetYaxis()->SetTitle("New Title");

//gPad->Update();
//gPad->Modified();

c->Update();
c->Modified();

}

No combination of the lines trying to update the canvas or using hs->GetHistogram()->GetYaxis() instead of hs->GetYaxis() results in the new y axis title being used.


Please read tips for efficient and successful posting and posting code

_ROOT Version:_6.18/04
Platform: Not Provided
Compiler:


Here is one way:

   hs->SetTitle("Title ;X axis; New Title");
   gPad->Modified();
   gPad->Update();

But I agree it should be possible to set the title on the axis, like this:

   hs->GetYaxis()->SetTitle("New Title");
   gPad->Modified();
   gPad->Update();

Let’s see what @couet think about it once is back from vacation

Thanks @bellenot, that works!

1 Like

Actually THStack get hist axis from an histogram create at drawing time.
The title (with the axis title separated by ;) if given to this histogram from the
THStack … so setting the axis title via GetXYZaxis is not easy (my be not possible).

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