Two simple challenges with TCanvas


I have two simple problems. They are so simple that I would expect a simple solutions. Though, I have not found any yet…

  1. I have a TCanvas divided into two TPads. I want to have statistics in one and not in the other. How can I do it?

  2. I have a TCanvas and want to (programatically) change axis labels. Is there an easy way?

Had you an idea, please let me know…

Cheers, Kaspi.

Answer to the first question:

   hid1 = new TH1F("hid1"," id1 ", 10, 0.5, 10.5);
   hid2 = new TH1F("hid2"," id1 ", 10, 0.5, 10.5);
   for(Int_t i=1; i<=10; i++) {
      hid1->Fill(i, 2.*i);
      hid2->Fill(i, 3.*i);

   TCanvas * c = new TCanvas("c","c",500,300);
   c->cd(1); hid1->Draw("");
   c->cd(2); hid2->Draw("");

2nd question:

what to you mean by " changing the axis labels ?"

Great, thanks! Still, I have a few comments.

  1. I would suggest to add SetStats() method to the pop-up menu which appears when a TH1 is right-clicked. This would allow user to switch on/off statistics boxes via GUI.

  2. I don not really understand the logic behind. The stat. box is one-to-one related to the histogram. I don’t see the point why the boxes’ display status is toggled via menu of a TCanvas. The user should have a possibility to (EASILY) select histograms to show statistics. Taking into account the facts that a TCanvas may have several TPads and a TPad may contain several TH1s, I would suggest the following: put a Stats on/off check box to the editor panel (associated with TH1).

  3. Finally, I’ve encountered redrawing problems. If I change stat. box status via TCanvas window menu, canvas itself is not redrawn. I have to some clicks around to get it redrawn.

Cheers, Jan.

[quote=“couet”]2nd question:

what to you mean by " changing the axis labels ?"[/quote]

An example. You load a TCanvas from a ROOT file and draw it. You see a plot with x-axis having label “E in GeV”. But you would prefer label “E (GeV)”. What do you do?

use the graphics editor, point to the axis and change the title.
You can generate the canvas.C file and see the relevant call to change
the axis tithe.


Thanks for answer.

Yes this works, but imagine you need to do that 50 times… That is why I asked how to do it “programatically” .

You can generate the canvas.C file and see the relevant call to change
the axis tithe.[/quote]
I did so and it boils down to changing axis title of an histogram. My question was quite different. You have a pointer to a TCanvas and you want to change axis titles. You don’t care (or you don’t want to care) what is plotted in TCanvas and just change the axis title. In my opinion, this is a simple task (moreover a frequent one) which should be accomplished by simple means. The only solution I am aware of is to take list of primitives, take the first entry, check it is an histogram and change its axis labels. And this is far from being easy…

Thanks, Kaspi.

see class TStyle. Add your own settings like axis title sizes to your gStyle.


Thanks Rene. I didn’t really mean changing properties of the titles. I meant changing the text itself. Something like the non-existing function SetAxisLabels in the following example:

TFile *file = new TFile(...);
TCanvas *can = (TCanvas *) file->Get(...);
can->SetAxisLabels("E  (GeV)", "count");
can->Draw(); // producing a plot with labels "E   (GeV)" and "count"

simply do:


hpx is an histogram.

Let me repeat the example:

TFile *file = new TFile(...); TCanvas *can = (TCanvas *) file->Get(...); can->SetAxisLabels("E (GeV)", "count");
The only thing you have is the pointer to TCanvas (you have no hpx to use!). Moreover you don’t want to bother about contents of the TCanvas. Wheter it comprises TH1x or TGraphs, how many, what is first, … The only desire is to modify axis labels.

The axis (TAxis) are objects part of the histogram (or graph) . The title axis title goes with axis (TAxis). The axis title aim is to give the legend to the data represented along the x,y or z axis. This title as nothing to do with the canvas, it is connected to the data set (histogram graph etc …). The link betwen these titles along the axis and the data is so strong that it is even possible to define them (the titles) in one go with the histogram title (see the convention). What you want is something not connected to the data at all. So you better use TLatex to paint such “titles”.

I strongly disagree.

You may create a histogram and give titles to its axis, but the titles become only important when you plot the histogram. (of course you can print it on screen, but who does it…) And hence there is also strong link between the title and the canvas.

A very practical example. I performed resolution studies for our detectors. I had 3 histograms: resolution in x, resolution in y and combined (radial) resolution. The first one was called “res_x” and had (horizontal) axis title “resolution in x”. I wanted to have all three histograms plotted in the same canvas (the best way to compare the results), hence I did res_x->Draw(); res_y->Draw("same"); res_c->Draw("same");
Indeed, the axis shown in canvas read “resolution in x”, which is relevant only for one of those three histograms. I wanted to change the title to “resolution” and explain the things in legend. Of course I could do res_x->GetXaxis()->SetTitle("resolution");
This would change the shown title appropriately, but res_x histogram itself would have incompatible axis title. You see the problem? In my opinion there is a strong dependence between canvas and axes.

One more argument. You have 1 canvas, 1 (horizontal) axis and arbitrary objects plotted. How can be that the axis is more related to object (which one from the list?) that to the canvas?

I think it is quite nice that TH1x object has a axis and its title, but TCanvas should definitely have also axes. Moreover those of TCanvas should be independent on those of TH1x.

What do you think?

You do not agree, but it is how it is implemented.

  1. TAxis blong to histograms
  2. The axis title drawn when you plot and histogram is part of TAxis.
  3. If you want to draw an “axis title” which is not the one stored in the histogram you should do it with TLatex.

[quote=“couet”]You do not agree, but it is how it is implemented.

Yes, this is my point. The logic of the implementation may be improved… Thanks anyway.

Cheers, Kaspi.