# Inset for same plot (histogram + TGraph)

**URL:** <https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213>\
**Category:** ROOT\
**Created:** [October 21, 2018, 6:48am UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213 "2018-10-21T06:48:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ameen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Ameen](https://root-forum.cern.ch/u/Ameen)\
**Post date:** [October 21, 2018, 6:48am UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/1 "2018-10-21T06:48:24Z")

</div>

Dear ROOT experts,

I superimposed a stack of histograms and a TGraph on the same plot with two different vertical axes. Now I want to zoom in into a particular x axis range and place it as an inset figure. I have seen many examples of an inset which is different from the actual main plot but none with the same situation as mine.  
How can I create an inset of the same plot but zoomed into a certain x axis range?

Thank you,  
Amin

* * *

\_ROOT Version: 6.08/06  
\_Platform: Ubuntu 16.04  
_Compiler:_ Not Provided

* * *

---

<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:** [October 22, 2018, 5:29am UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/2 "2018-10-22T05:29:34Z")

</div>

> [@Ameen](#):
>
> How can I create an inset of the same plot but zoomed into a certain x axis range?

```auto
{
  TCanvas *c1 = new TCanvas("c1","c1",200,10,700,500);

  TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);

  gRandom->SetSeed();
  Float_t px, py;
  for (Int_t i = 0; i < 25000; i++) {
     gRandom->Rannor(px,py);
     hpx->Fill(px);
  }
  hpx->Draw();

  TPad *p1 = new TPad("p1","p1",.15,.3,.5,.7);
  p1->Draw();
  p1->cd();
  TH1F *hc = (TH1F *)hpx->DrawCopy();
  hc->GetXaxis()->SetRangeUser(0,1);
}

```

---

<div class="post-metadata">

**Author:** ![Ameen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Ameen](https://root-forum.cern.ch/u/Ameen)\
**Post date:** [October 22, 2018, 4:07pm UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/3 "2018-10-22T16:07:22Z")

</div>

Dear @couet,

Thank you for the reply. I should have given more info as this doesn’t exactly work for me.  
I have a bunch of background histograms which I read from a separate ROOT file. I draw them (with no stack) and then draw the signal histogram on top (also read from a ROOT file). The structure of the code looks like this:

```
THStack *hs = new THStack("hs",title);
.
.
hs->Add(h[iprocess]);
TCanvas *c = new TCanvas("c","Stacked Background Hists");
hs->Draw("HIST NOSTACK");
.
.
TH1 *signalh = (TH1F*)signalf->Get(histoname);
signalh->Draw("HIST SAME");

```

THStack does not have the DrawCopy() option. Is there a way around that?

Thank you,  
Amin

---

<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:** [October 23, 2018, 7:48am UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/4 "2018-10-23T07:48:39Z")

</div>

What do you want in the inset ? `signalh` ?

---

<div class="post-metadata">

**Author:** ![Ameen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Ameen](https://root-forum.cern.ch/u/Ameen)\
**Post date:** [October 23, 2018, 10:48am UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/5 "2018-10-23T10:48:41Z")

</div>

Hi @couet,

I want both signalh and the hs (stack).

Thank you,  
Amin

---

<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:** [October 23, 2018, 11:13am UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/6 "2018-10-23T11:13:22Z")

</div>

So the stack hs should appear in the main plot and in the inset ? that’s it ?

---

<div class="post-metadata">

**Author:** ![Ameen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Ameen](https://root-forum.cern.ch/u/Ameen)\
**Post date:** [October 23, 2018, 12:45pm UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/7 "2018-10-23T12:45:09Z")

</div>

Hi @couet,

Yes that is correct.

---

<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:** [October 24, 2018, 8:15am UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/8 "2018-10-24T08:15:54Z")

</div>

```auto
{
   TCanvas *c1 = new TCanvas("c1","c1",200,10,700,500);

   TH1F *hpx = new TH1F("hpx","This is the px distribution",100,-4,4);

   THStack *hs = new THStack();
   hs->Add(hpx);

   gRandom->SetSeed();
   Float_t px, py;
   for (Int_t i = 0; i < 25000; i++) {
      gRandom->Rannor(px,py);
      hpx->Fill(px);
   }
   hs->Draw();

   TPad *p1 = new TPad("p1","p1",.15,.3,.5,.7);
   p1->Draw();
   p1->cd();
   p1->DrawFrame(0.,500.,1.,800);
   hpx->Draw("SAME");
}

```

---

<div class="post-metadata">

**Author:** ![Ameen](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@Ameen](https://root-forum.cern.ch/u/Ameen)\
**Post date:** [October 24, 2018, 2:57pm UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/9 "2018-10-24T14:57:51Z")

</div>

Thank you very much. It works!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [November 7, 2018, 2:57pm UTC](https://root-forum.cern.ch/t/inset-for-same-plot-histogram-tgraph/31213/10 "2018-11-07T14:57:51Z")

</div>

Closed on November 7, 2018, 2:57pm UTC

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