# Drawing Graphs with an offset in Y axis

**URL:** <https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490>\
**Category:** Newbie\
**Tags:** root\
**Created:** [June 20, 2022, 11:57pm UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490 "2022-06-20T23:57:33Z")\
**Posts on this page:** 16\
**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 20, 2022, 11:57pm UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/1 "2022-06-20T23:57:33Z")

</div>

Hello Rooters,

I am stuck at a step which I am pretty sure must have a straightforward fix. I have tried a few things and I am not sure what to do.  
I have multiple graphs. I want to draw all these graphs in a single plot with an offset on Y-axis, such that every plot has a shift in Y-axis. This is what I did -

TGraph \*gr2 = new TGraph ();

TCanvas \*cgr2=new TCanvas(“cgr2”,“cExcitation Energy Spectrum 126”);

for(Int\_t i=0; i\< 21; i++){

```
    double energy = grdisk126->GetX() [i];

    double Counts = grdisk126->GetY() [i];

    Int_t shift = Counts + 1000; //CHECK DATA

    grdisk126->SetPoint(i, energy, shift);

    }

```

cgr2-\>cd();

gr2-\>Draw();  
…  
TMultiGraph \*mg = new TMultiGraph();

TCanvas \*cmg=new TCanvas(“cmg”," Excitation Energy Spectrum all data stacked");

mg-\>Add(grdisk118);

mg-\>Add(grdisk126);  
…

This does not work and gives me an error- error in tgraphpainter::paintgraph: illegal number of points (0).

Can anyone suggest anything as to how to approach this? I want something that looks like the attached pdf.  
[Stacked plots-Logscale\_1000bins.pdf](https://root-forum.cern.ch/uploads/short-url/rTjlZ4Ys0cZj3fY0G8LX21yJ1tY.pdf) (219.8 KB)

Please help! Thank you for your time!

_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 21, 2022, 6:13am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/2 "2022-06-21T06:13:56Z")

</div>

Your “`gr2`” seems to be empty (you did not add any points).

---

<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 21, 2022, 6:39am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/3 "2022-06-21T06:39:53Z")

</div>

Can you post a running example showing the problem? what you post can not be run,

---

<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 22, 2022, 6:38am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/4 "2022-06-22T06:38:00Z")

</div>

Hi @couet and @Wile_E_Coyote, Thank you for getting back. Here is a code that will run. I am also attaching the .root files because I am plotting .root files.

Also, I tried to plot the same plot in graph and as histogram. What I don’t understand is when I plot the histogram, everything lines up exactly (still having trouble with offset though), but when I plot as graph, there is slight shift in each data. Is that something that has to do with how THStack and multigraph works or is it some error in my code?  
Can anyone please comment? Thank you for your time! Please help!

[Disk118\_graph.root](https://root-forum.cern.ch/uploads/short-url/2vq7ioxFhZcXQnovtguehOMf5fa.root) (11.6 KB)  
[Disk126\_graph.root](https://root-forum.cern.ch/uploads/short-url/lWVA6UVwZ0XUkoLmXEM5kzrIVWJ.root) (11.5 KB)  
[Disk179\_graph.root](https://root-forum.cern.ch/uploads/short-url/gEAWnanCm8wcRLyZNIMxfVbPTll.root) (11.5 KB)

[Overlay.cc](https://root-forum.cern.ch/uploads/short-url/c0welsUqWZ5PrbWiBIiF2cKEdGD.cc) (5.2 KB)  
[OverlayGraph.cc](https://root-forum.cern.ch/uploads/short-url/k4D9EIh6mdLtM5ZK1tdrfU1vgbl.cc) (6.6 KB)  
[Disk118.root](https://root-forum.cern.ch/uploads/short-url/89zFLDog8gWBLbcPWyIch5fWzB0.root) (5.3 KB)  
[Disk126.root](https://root-forum.cern.ch/uploads/short-url/zg8uAK7sZVjq8vVPWmKQBSKyOV5.root) (5.3 KB)  
[Disk179.root](https://root-forum.cern.ch/uploads/short-url/ewLwDOWMFJRQqQVomsSfYiCyLz1.root) (5.3 KB)  
[Disk186.root](https://root-forum.cern.ch/uploads/short-url/mF6Z8nMpStNBcGP5dKHCNTSWEQj.root) (5.2 KB)  
[Disk192.root](https://root-forum.cern.ch/uploads/short-url/aahRHvwWSpxLSq6ZmP1ausHKBJT.root) (5.1 KB)  
[Disk211.root](https://root-forum.cern.ch/uploads/short-url/59lDDveDBStsm8IEmk0V1k8Nl6J.root) (5.2 KB)  
[Disk215.root](https://root-forum.cern.ch/uploads/short-url/daSL1opElh2wVLtmfDk2cD2bIpL.root) (5.3 KB)  
[Disk186\_graph.root](https://root-forum.cern.ch/uploads/short-url/dHavfyyryKsCULkvmpntxYN76bh.root) (11.5 KB)  
[Disk192\_graph.root](https://root-forum.cern.ch/uploads/short-url/yYkTXs90XKpIuQJsdhhOPOORGQg.root) (11.4 KB)  
[Disk211\_graph.root](https://root-forum.cern.ch/uploads/short-url/flvIEw1ZzlqHmwxdCSpLxOK17g5.root) (11.4 KB)  
[Disk215\_graph.root](https://root-forum.cern.ch/uploads/short-url/mHr4WnU4x4QVsxhzp8dhpm5FDtC.root) (11.6 KB)

---

<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 22, 2022, 7:21am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/5 "2022-06-22T07:21:22Z")

</div>

Can you clean up the macro so it runs with all the file in one folder ?

```auto
Processing Overlay.cc...
Error in <TFile::TFile>: file /root_v6.18.04/macros/HandOff/AlThin/Next steps/Disk118.ROOT does not exist

```

---

<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 22, 2022, 7:28am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/6 "2022-06-22T07:28:08Z")

</div>

Do you mean something like this? (See attachments  
[Overlay.cc](https://root-forum.cern.ch/uploads/short-url/umdVNB6lQg8TXO6vBxShN19rbCA.cc) (4.8 KB)  
[OverlayGraphs.cc](https://root-forum.cern.ch/uploads/short-url/guCX8EsyhO0H8BiTa2kc638Z1cl.cc) (6.2 KB)  
)

---

<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 22, 2022, 9:07am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/7 "2022-06-22T09:07:54Z")

</div>

Yes with this new version work. I did:

```auto
$ root Overlay.cc

```

I get:

 ![Screenshot 2022-06-22 at 11.05.04](https://root-forum.cern.ch/uploads/default/original/3X/e/c/ec5a5f505751ced933e0360c2664f5fd36b80fa9.png)

does that show the problem ?

---

<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 22, 2022, 5:32pm UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/8 "2022-06-22T17:32:19Z")

</div>

Hi @couet,  
Thank you for getting back. So what I am saying is, this is stack histogram where different data sets are stacked. IN this plot, (the one you attached), all the peaks are perfectly aligned.  
However, when I plot a multigraph using the same data, all the peaks are slightly shifted towards right from top to bottom(see attachment).  
My question is there a reason why the histograms are aligned and graphs are shifted along x axis despite being the same data?

Do you think you can help? Any help is appreciated! Thank you for your time

 ![2022-06-22](https://root-forum.cern.ch/uploads/default/original/3X/3/a/3ab0893b548ca83962716af3c4f9c12103f81c88.png)

P.S - Please ignore the offset in yaxis. I want to plot all the data with an offset in Y. For some reason, the offset is not getting plotted for the histogram but works for graphs.

---

<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 23, 2022, 7:12am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/9 "2022-06-23T07:12:03Z")

</div>

I see . I might be a binning effect or something else. It should be checked…

---

<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 23, 2022, 7:44am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/10 "2022-06-23T07:44:53Z")

</div>

In your “`Overlay.cc`”, you need: `hdiskXYZ->SetBinContent(i, shift);`  
And you should probably draw it using: `hs->Draw("hist nostack");`

BTW. It can be that the x-axis values of your graphs, that you use in your “`OverlayGraphs.cc`”, are not “calibrated”.

---

<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 23, 2022, 8:00am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/11 "2022-06-23T08:00:19Z")

</div>

```auto
void Overlay()
{

   // This part opens the .ROOT file and plots them separately. The plots are saved as .ROOT files in the original DataAnalysis.Diskno.cc programs
   // This program will only work for previously saved .root files.
   TFile* inp = TFile::Open("Disk118.ROOT");
   TH1F* hdisk118 = (TH1F*)inp->Get("Disk118");

   TFile* inp1 = TFile::Open("Disk126.ROOT");
   TH1F* hdisk126 = (TH1F*)inp1->Get("Disk126");

   TFile* inp2 = TFile::Open("Disk179.ROOT");
   TH1F* hdisk179 = (TH1F*)inp2->Get("Disk179");

   TFile* inp3 = TFile::Open("Disk186.ROOT");
   TH1F* hdisk186 = (TH1F*)inp3->Get("Disk186");

   TFile* inp4 = TFile::Open("Disk192.ROOT");
   TH1F* hdisk192 = (TH1F*)inp4->Get("Disk192");

   TFile* inp5 = TFile::Open("Disk211.ROOT");
   TH1F* hdisk211 = (TH1F*)inp5->Get("Disk211");

   TFile* inp6 = TFile::Open("Disk215.ROOT");
   TH1F* hdisk215 = (TH1F*)inp6->Get("Disk215");

  // Now plotting all these graphs in a TGraph in a single plot with an offset

   THStack *hs = new THStack("hs","Excitation Energy vs Counts Spectrum_All data stacked");
   hs->Add(hdisk118);

   for(Int_t i=0; i< 801; i++){
   double ex = hdisk126->GetBinCenter(i);
   double Counts = hdisk126->GetBinContent(i);
   Int_t shift = Counts + 1000; //CHECK DATA
   hdisk126->SetBinContent(i, shift);
   }
   hdisk126->SetLineColor(kRed);
   hs->Add(hdisk126);

   for(Int_t i=0; i< 801; i++){
      double ex = hdisk179->GetBinCenter(i);
      double Counts = hdisk179->GetBinContent(i);
      Int_t shift = Counts + 2000; //CHECK DATA
      hdisk179->SetBinContent(i, shift);
   }
   hdisk179->SetLineColor(kGreen);
   hs->Add(hdisk179);

   for(Int_t i=0; i< 801; i++){
      double ex = hdisk186->GetBinCenter(i);
      double Counts = hdisk186->GetBinContent(i);
      Int_t shift = Counts + 3000; //CHECK DATA
      hdisk186->SetBinContent(i, shift);
   }
   hdisk186->SetLineColor(kBlack);
   hs->Add(hdisk186);

   for(Int_t i=0; i< 801; i++){
      double ex = hdisk192->GetBinCenter(i);
      double Counts = hdisk192->GetBinContent(i);
      Int_t shift = Counts + 4000; //CHECK DATA
      hdisk192->SetBinContent(i, shift);
   }
   hdisk192->SetLineColor(kPink);
   hs->Add(hdisk192);

   for(Int_t i=0; i< 801; i++){
      double ex = hdisk211->GetBinCenter(i);
      double Counts = hdisk211->GetBinContent(i);
      Int_t shift = Counts + 5000; //CHECK DATA
      hdisk211->SetBinContent(i, shift);
   }
   hdisk211->SetLineColor(9); // This is purple
   hs->Add(hdisk211);

   for(Int_t i=0; i< 801; i++){
      double ex = hdisk215->GetBinCenter(i);
      double Counts = hdisk215->GetBinContent(i);
      Int_t shift = Counts + 6000; //CHECK DATA
      hdisk215->SetBinContent(i, shift);
   }
   hdisk215->SetLineColor(kYellow);
   hs->Add(hdisk215);

   TCanvas *cst = new TCanvas("cst","stacked hists");

   hs->Draw("hist");

   hs->GetXaxis()->SetTitle("Excitation Energy (MeV)");
   hs->GetYaxis()->SetTitle("Counts");
   hs->GetXaxis()->SetRangeUser(10,18);
   gPad->Modified();
   gPad->Update();
   hs->SaveAs("Excitation Energy vs Counts Spectrum_All data stacked.pdf");

}

```

 ![Screenshot 2022-06-23 at 09.59.30](https://root-forum.cern.ch/uploads/default/original/3X/f/b/fba6094e814b5af5b0de998b7027d15af8ab9131.png)

---

<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 23, 2022, 8:02am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/12 "2022-06-23T08:02:18Z")

</div>

@couet Maybe: `hs->Draw("hist nostack");`

---

<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 23, 2022, 8:13am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/13 "2022-06-23T08:13:48Z")

</div>

We had the same idea at the same time.  
` hs->Draw("hist nostack");`  
gives:

 ![Screenshot 2022-06-23 at 10.12.39](https://root-forum.cern.ch/uploads/default/original/3X/1/3/13f63a31447f754ced8e281fddbc47f432528f86.png)  
so the data in both histograms and graphs are not calibrated.

---

<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 26, 2022, 6:17am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/14 "2022-06-26T06:17:24Z")

</div>

Thank you for getting back @couet and @Wile_E_Coyote. I just had one more question. What does it mean by the histogram and graph “not calibrated”?  
Could you please suggest to me where to look for how to do a calibration? Thank you so much for your time and help!  
I really appreciate it!

---

<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 26, 2022, 10:29am UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/15 "2022-06-26T10:29:23Z")

</div>

The positions of peaks in your different graphs / histograms do not “coincide”.  
Well, we assume that their “x” positions should be the same in all graphs / histograms, but maybe not, as it depends on “physics”.  
So, we think that you probably need to “scale” the x-axes values of them (i.e., “calibrate” the x-axes of your graphs / histograms from “raw” numbers to “energy”).  
You need to discuss it with the one who gave you this data.

---

<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 28, 2022, 9:01pm UTC](https://root-forum.cern.ch/t/drawing-graphs-with-an-offset-in-y-axis/50490/16 "2022-06-28T21:01:02Z")

</div>

Thank you @Wile_E_Coyote. This was very helpful! Appreciate your time!
