# Help with plotting multiple histograms in one plot with logarithmic scale with an offset

**URL:** <https://root-forum.cern.ch/t/help-with-plotting-multiple-histograms-in-one-plot-with-logarithmic-scale-with-an-offset/50346>\
**Category:** Newbie\
**Tags:** hist, root\
**Created:** [June 9, 2022, 6:52pm UTC](https://root-forum.cern.ch/t/help-with-plotting-multiple-histograms-in-one-plot-with-logarithmic-scale-with-an-offset/50346 "2022-06-09T18:52:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nisha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nisha/32/28801_2.png) [@Nisha](https://root-forum.cern.ch/u/Nisha)\
**Post date:** [June 9, 2022, 6:52pm UTC](https://root-forum.cern.ch/t/help-with-plotting-multiple-histograms-in-one-plot-with-logarithmic-scale-with-an-offset/50346/1 "2022-06-09T18:52:17Z")

</div>

Hello Root Experts,

I am trying to plot multiple histograms in one plot. I have converted the yaxis of each histogram to logscale individually. However, when I try to plot all these histograms in one single plot, I do not get the logscaled yaxis, instead, I just get the normal y axis in the stacked plot.  
Also, I wanted to plot each of these histograms in logscale with a small offset on y axis. Is there a was of doing that?  
Here is what I was trying to do-

TH1F \*hStackedPlots = new TH1F(“hStackedPlots”, “Excitation Energy Vs Counts”,801,0,20);

TCanvas \*cStackedPlots = new TCanvas(“cStackedPlots”, “cStackedPlots”);

cStackedPlots-\>cd();

```
hStackedPlots->Draw();

hStackedPlots->Add(hdisk118);

hStackedPlots->Draw("hist 1 same");

cStackedPlots->Update();

//cStackedPlots->SetLogy();

hdisk126->Draw("hist 1 same");

hdisk126->SetLineColor(kRed);

cStackedPlots->Update();

```

I also tried using a for loop for the logscale offset by multiplying a constant to each of the bincontent to induce a shift in yaxis, but nothing seems to work.  
I am sure there is a smart way to do this. Can anyone help? Thank you for your time and help! Appreciate it!

Nisha

_ROOT Version:_ 6.18/04  
_Platform:_ C++  
_Compiler:_ Visual Studio Code

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 9, 2022, 7:15pm UTC](https://root-forum.cern.ch/t/help-with-plotting-multiple-histograms-in-one-plot-with-logarithmic-scale-with-an-offset/50346/2 "2022-06-09T19:15:38Z")

</div>

[THStack](https://root.cern/doc/master/classTHStack.html)

---

<div class="post-metadata">

**Author:** ![Nisha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nisha/32/28801_2.png) [@Nisha](https://root-forum.cern.ch/u/Nisha)\
**Post date:** [June 9, 2022, 9:11pm UTC](https://root-forum.cern.ch/t/help-with-plotting-multiple-histograms-in-one-plot-with-logarithmic-scale-with-an-offset/50346/3 "2022-06-09T21:11:42Z")

</div>

Hi @Wile_E_Coyote, Thank you for getting back so quickly. This really helped. I just have one question.

When I stack the log scale plots with an offset, the yaxis is not getting converted to log when I try to stack them. The individual plots do plots by logscale though. This is what I wrote-

```auto
// Plotting individual plots first-
TFile* inp = TFile::Open("/root_v6.18.04/macros/HandOff/AlThin/Next steps/Disk118.ROOT");

    inp->ls();

    TH1F* hdisk118 = (TH1F*)inp->Get("Disk118");

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

    cdisk118->cd();

    cdisk118->SetLogy();

    hdisk118 -> Draw("hist");
... <doing the same for other plots >...
THStack *hs = new THStack("hs","Excitation Energy vs Counts Spectrum_All data stacked");

 hs->Add(hdisk118);

 for(Int_t i=0; i< 801; i++){

        Int_t channel = i;

        double Counts = hdisk126->GetBinContent(i);

        Int_t shift = Counts + 1000; //Giving offset

        hdisk126->SetBinContent(channel, shift);

        }

 hdisk126->SetLineColor(kRed);

 hs->Add(hdisk126);

```

This does give the offset but the y-axis is still not in logscale for some reason. I know it must be a silly mistake. Can you please help?  
Thank you for your time and help. Really appreciate it!

Nisha

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 10, 2022, 6:10am UTC](https://root-forum.cern.ch/t/help-with-plotting-multiple-histograms-in-one-plot-with-logarithmic-scale-with-an-offset/50346/4 "2022-06-10T06:10:37Z")

</div>

Can you post a running macro reproducing the problem? I do not see any thin obviously wrong in the piece of code you posted. You do not `Draw` the stack. is it intended?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [June 10, 2022, 6:50am UTC](https://root-forum.cern.ch/t/help-with-plotting-multiple-histograms-in-one-plot-with-logarithmic-scale-with-an-offset/50346/5 "2022-06-10T06:50:43Z")

</div>

You should have something like this:

```cpp
hs->Draw("HIST NOSTACK");
gPad->SetLogy(1);

```

---

<div class="post-metadata">

**Author:** ![Nisha](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/nisha/32/28801_2.png) [@Nisha](https://root-forum.cern.ch/u/Nisha)\
**Post date:** [June 13, 2022, 8:25pm UTC](https://root-forum.cern.ch/t/help-with-plotting-multiple-histograms-in-one-plot-with-logarithmic-scale-with-an-offset/50346/6 "2022-06-13T20:25:33Z")

</div>

Thank you @Wile_E_Coyote.  
gPad-\>SetLogy(1);  
This worked like magic.
