Drawing two 1D histograms with different binning and X-ranges on one Canvas

Greetings, rooters!
I encountered a problem while trying to plot two 1D histograms with different binning, binwidth, and overlapping X-ranges (one is 2.4 - 3.0 MeV and the other is 2.7 - 3.4 MeV) on one Canvas (to show two graphs on one picture). What I tried is:

  1. “same” option draws two histograms using only X-range of the first histo, when I want to see the whole range.
  2. “nostack” option is not a solution either because it works the same way as “same” option.
    I didn’t succeed in finding any example on root tutorials which could help me solve this problem.
    Maybe, some of you have faced the same issue. I’d be very happy to have any help.

You need to create a THStack object (and then draw it with “nostack” or “nostackb”).

“If option “nostack” is specified the histograms are not drawn on top of each other but as they would if drawn using the option “same”.” I have already tried this. “nostack” option works the same way as “same” option, so it will not show me the whole range, but only the range of the first histogram.

I have 2 idea for you:

  1. Set the range of both histograms to be equal, in this case you have to change also the number of bins of your histograms to have the bin width that you desire.
  2. Before drawing the first histogram do

Then you can draw h and h2


Thank you, Stefano, for response.

  1. I don’t want to set equal ranges. I want to see two histograms each having its range to show the whole picture.
  2. h->GetXaxis()->SetRangeUser(2.4,3.4) will not help in this case. If my first histogram’s X-range is 2.4 -3.0, setting (2.4, 3.4) X axis range will still show only (2.4,3.0) range part, because SetRangeUser(lower,upper) method computes the closest bin to upper and lower limits in histogram that I set.

you can create a new histogram h3 with range from 2.4 to 3.4 also with just one bin, don’t fill it and then h3 should be the first histogram you draw, then superimpose the others two histogram.
For sure you should adjust the y-axis after you plotted everything, but in the end will work.


May be you can provide a small script reproducing the problem. It will be easier to help you by modifying it in order to make it work.

Thank you, Stefano!
The problem was solved and solution was a recipe similar to what you’ve recommended.

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