Changing the scale for graph?

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 KY(X) or Y(KX), 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.

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

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

Here are some references on how I obtained that code:
[url]Feature Request: Transform TAxes

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.


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); }

1 Like