# Plotting difference between data and fit on the same plot for Cs-137 spectrum

**URL:** <https://root-forum.cern.ch/t/plotting-difference-between-data-and-fit-on-the-same-plot-for-cs-137-spectrum/39869>\
**Category:** Newbie\
**Created:** [June 11, 2020, 2:39am UTC](https://root-forum.cern.ch/t/plotting-difference-between-data-and-fit-on-the-same-plot-for-cs-137-spectrum/39869 "2020-06-11T02:39:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [June 11, 2020, 2:39am UTC](https://root-forum.cern.ch/t/plotting-difference-between-data-and-fit-on-the-same-plot-for-cs-137-spectrum/39869/1 "2020-06-11T02:39:33Z")

</div>

Hello,

I am trying to plot on the existing plot the difference between the fitted function and the histogram. I thought it was as simple as including the line `h->Add(g,-1);` but then my existing plot gets distorted. The macro works fine up until that line. I am very confused and any help would be greatly appreciated.  
[comp.cxx](https://root-forum.cern.ch/uploads/short-url/iYAXnPQSGiRJp5VuJa48vGnkE7L.cxx) (1013 Bytes) [Compton.txt](https://root-forum.cern.ch/uploads/short-url/nF2iistqRYTvdmP2ChLIr3Ad1Dn.txt) (329.3 KB)  
\_ROOT Version: 6.23/01

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 11, 2020, 7:48am UTC](https://root-forum.cern.ch/t/plotting-difference-between-data-and-fit-on-the-same-plot-for-cs-137-spectrum/39869/2 "2020-06-11T07:48:41Z")

</div>

Maybe @couet or @moneta cang give an advice, but why not using a ratio plot for this? Something like:

```auto
   auto rp1 = new TRatioPlot(h);
   rp1->Draw();
   rp1->GetLowerRefGraph()->SetMinimum(-5);
   rp1->GetLowerRefGraph()->SetMaximum(5);

```

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [June 11, 2020, 8:47am UTC](https://root-forum.cern.ch/t/plotting-difference-between-data-and-fit-on-the-same-plot-for-cs-137-spectrum/39869/3 "2020-06-11T08:47:27Z")

</div>

Yes, using the TRatioPlot as shown above it is the correct way for plotting the fit residuals.  
If you have any issues using TRatioPlot please let us know

Best regards

Lorenzo

---

<div class="post-metadata">

**Author:** ![Edward\_Finkelstein](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/edward_finkelstein/32/20070_2.png) [@Edward\_Finkelstein](https://root-forum.cern.ch/u/Edward_Finkelstein)\
**Post date:** [June 11, 2020, 6:28pm UTC](https://root-forum.cern.ch/t/plotting-difference-between-data-and-fit-on-the-same-plot-for-cs-137-spectrum/39869/4 "2020-06-11T18:28:49Z")

</div>

Thank you very much!
