# Changing the scale for graph?

**URL:** <https://root-forum.cern.ch/t/changing-the-scale-for-graph/17339>\
**Category:** ROOT\
**Created:** [March 11, 2014, 3:59pm UTC](https://root-forum.cern.ch/t/changing-the-scale-for-graph/17339 "2014-03-11T15:59:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyrus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@Cyrus](https://root-forum.cern.ch/u/Cyrus)\
**Post date:** [March 11, 2014, 3:59pm UTC](https://root-forum.cern.ch/t/changing-the-scale-for-graph/17339/1 "2014-03-11T15:59:46Z")

</div>

Hi everyone, I’m drawing a plot from file using the standart

` TGraph *gr1=new TGraph("Filename.txt","%lg %lg");
gr1->Draw("ALP")
`  
procedure, but I can’t find a way to change the scale. I.e. if I want not an Y(X) plot, but a K_Y(X) or Y(K_X), where the K is constant, and X and Y are written in the .txt file, what should I do (without rewriting the .txt file)? I think it’s a trivial question, but I wasn’t able to find this somewhere.

---

<div class="post-metadata">

**Author:** ![jfcaron](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/j/7c8e57/32.png) [@jfcaron](https://root-forum.cern.ch/u/jfcaron)\
**Post date:** [March 11, 2014, 7:12pm UTC](https://root-forum.cern.ch/t/changing-the-scale-for-graph/17339/2 "2014-03-11T19:12:09Z")

</div>

I have a PyROOT function which accomplishes the X-axis rescaling, you could re-write it in C++ (basically add curly braces and types).

```auto
def rescaleaxis(g,scale=50e-12/1e-9):
    """This function rescales the x-axis on a TGraph."""
    N = g.GetN()
    x = g.GetX()
    for i in range(N):
        x[i] *= scale
    g.GetHistogram().Delete()
    g.SetHistogram(0)
    return
```

Here are some references on how I obtained that code:  
[url][Feature Request: Transform TAxes](https://root-forum.cern.ch/t/feature-request-transform-taxes/16327/1)  
[root.cern.ch/phpBB3/viewtopic.php?f=3&t=15953](http://root.cern.ch/phpBB3/viewtopic.php?f=3&t=15953)

For the Y-axis, I think you can do the same thing but with GetY(). I tried doing another trick by converting to a TH1, TH1::Scale, then converting back to TGraph, but it didn’t work for some reason. I’ll admit I didn’t try very hard to make this trick work.

Jean-François

---

<div class="post-metadata">

**Author:** ![Cyrus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/ac91a4/32.png) [@Cyrus](https://root-forum.cern.ch/u/Cyrus)\
**Post date:** [March 25, 2014, 3:23pm UTC](https://root-forum.cern.ch/t/changing-the-scale-for-graph/17339/3 "2014-03-25T15:23:12Z")

</div>

Thank you very much (sorry for late response). This is how the code would look for C++:

`
void rescaleaxis(TGraph *g,double scale)
{
 int N=g->GetN();
 double *y=g->GetY();
 int i=0;
 while(i<N) 
 {
 y[i]=y[i]*scale;
 i=i+1;
 }
 g->GetHistogram()->Delete();
 g->SetHistogram(0);
 }`
