# How do I stretched a histogram along x-axis?

**URL:** <https://root-forum.cern.ch/t/how-do-i-stretched-a-histogram-along-x-axis/34759>\
**Category:** Newbie\
**Tags:** hist\
**Created:** [June 21, 2019, 9:31pm UTC](https://root-forum.cern.ch/t/how-do-i-stretched-a-histogram-along-x-axis/34759 "2019-06-21T21:31:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kbo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kbo](https://root-forum.cern.ch/u/kbo)\
**Post date:** [June 21, 2019, 9:31pm UTC](https://root-forum.cern.ch/t/how-do-i-stretched-a-histogram-along-x-axis/34759/1 "2019-06-21T21:31:25Z")

</div>

I have a 1D histogram. I want to stretched it along xaxis. How do I do that?

---

<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 22, 2019, 7:05am UTC](https://root-forum.cern.ch/t/how-do-i-stretched-a-histogram-along-x-axis/34759/2 "2019-06-22T07:05:50Z")

</div>

What do you mean by “stretch”?  
Is the histogram already created and filled?  
Maybe you just want to “scale” the x-axis?  
In any case, if you want to increase the number of bins, you will need to create a completely new histogram.

---

<div class="post-metadata">

**Author:** ![kbo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kbo](https://root-forum.cern.ch/u/kbo)\
**Post date:** [June 22, 2019, 12:16pm UTC](https://root-forum.cern.ch/t/how-do-i-stretched-a-histogram-along-x-axis/34759/3 "2019-06-22T12:16:49Z")

</div>

Yes I have a histogram created and Filled. Rebining will just combine bins in to less number. Stretching is just like splitting into more number of bins. I want to increase x value of histogram by a constant factor.

---

<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 22, 2019, 7:14pm UTC](https://root-forum.cern.ch/t/how-do-i-stretched-a-histogram-along-x-axis/34759/4 "2019-06-22T19:14:20Z")

</div>

```auto
{
  TH1F *h_narrow = new TH1F("h_narrow", "h_narrow", 200, -1., 1.);
  h_narrow->FillRandom("gaus");
  // make sure that the new histogram has "compatible" bins' widths
  TH1F *h_wide = new TH1F("h_wide", "h_wide", 1000, -5., 5.);
  h_wide->Sumw2(kTRUE); // ensure that the errors are properly "transfered"
  h_wide->Add(h_narrow);
  // h_wide->ResetStats(); // may be needed sometimes
  h_wide->Draw();
}

```

---

<div class="post-metadata">

**Author:** ![kbo](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/f475e1/32.png) [@kbo](https://root-forum.cern.ch/u/kbo)\
**Post date:** [June 23, 2019, 12:34am UTC](https://root-forum.cern.ch/t/how-do-i-stretched-a-histogram-along-x-axis/34759/5 "2019-06-23T00:34:50Z")

</div>

```auto
# include "TGraph.h"
#include "TCanvas.h"
Double_t ScaleX(Double_t x)
{
  Double_t v;
  v=x*1.5;
  return v;
}

void ScaleAxis(TAxis *a)
  {
    if (!a) return;
    if (a->GetXbins()->GetSize())
      {
	TArrayD X(*(a->GetXbins()));
	for (int i = 0; i<X.GetSize(); i++) X[i] = ScaleX(X[i]);
	a->Set((X.GetSize()-1),X.GetArray());
      }
  }
void ScaleXaxis(TH1D *h)
  
   {
     if (!h) return;
     ScaleAxis(h->GetXaxis());
     return;
   }
void graph()
{

  TH1F *h1 = new TH1F("h1","h1",100,-5,5);
  TH1F *h2 = new TH1F("h2","h2",100,-5,5);
  h1->FillRandom("gaus");
  h2->FillRandom("gaus");

 TCanvas *c =new TCanvas("c","c",1500,1500);
 c->Divide(2,1);
  c->cd(1);
 h1->Draw();
  h2->Draw("same");

 c->cd(2);
  ScaleXaxis(h1);
  h1->ResetStats();
  h1->Draw();

```

I think this above code should stretch my histogram. I have defined all necessary functions and stretching parameter.  
Can you look why my this code is not working?  
This code is supposed to draw original histogram in pad 1 and stretched one in pad 2.  
Thank You

---

<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, 2019, 6:09am UTC](https://root-forum.cern.ch/t/how-do-i-stretched-a-histogram-along-x-axis/34759/6 "2019-06-23T06:09:55Z")

</div>

[Both your histograms have “fix bins”.](https://root-forum.cern.ch/t/can-we-shift-histogram-for-several-channels/12050/2)
