Reverse x axis, TGraph

Hello,

to do a “simple” x:y plot of some data (read into a tree) I use TGraph. This works fine, but I want to reverse the x-axis. I can already reverse the direction, but I want as well that TGraph draws the plot from higher x-values to lower x-values. How does this work?

My code looks like this:

#include <TTree.h>
#include <TCanvas.h>
#include <TPad.h>
#include <TFile.h>
#include <TGraph.h>
#include <TGaxis.h>

void graph() {
// save as root file
    TFile* file = new TFile("data07.root", "RECREATE");
    file->cd();

// create Canvas
    gROOT->SetStyle("Plain");
    TCanvas *c1 = new TCanvas("c1","");

// read data in tree
    TTree *tree = new TTree("data","");
    tree->ReadFile("data.txt","TC:L");
    int size = tree->GetEntries();
    tree->SetEstimate(size);

// draw data as TGraph
    tree->Draw("TC:L");
    TGraph *g = new TGraph(size,tree->GetV1(),tree->GetV2());

    g->GetYaxis()->SetTitle("L");
    g->GetYaxis()->CenterTitle(1);
    g->GetXaxis()->SetTitle("T");
    g->GetXaxis()->CenterTitle(1);
    g->SetLineColor(1);        
    g->Draw("al");

// remove the current axis
    g->GetXaxis()->SetLabelOffset(999);
    g->GetXaxis()->SetTickLength(0);
// redraw the new axis
    gPad->Update();
    TGaxis *newaxis = new TGaxis(gPad->GetUxmax(),
                                gPad->GetUymin(),
                                gPad->GetUxmin(),
                                gPad->GetUymin(),
                                g->GetXaxis()->GetXmin(),
                                g->GetXaxis()->GetXmax(),
                                510,"-");
    newaxis->SetLabelOffset(-0.03);

    newaxis->Draw();

    c1->Write();
    c1->SaveAs("data07.eps");
}

Thanks a lot!
Boginja

This in on our to do list.
Right now you have to reverse the vectors yourself.

1 Like

Thanks!
And how can I do this? Do you have a small example how to reverse the vectors?

Cheers,
Boginja

You do a simple “for” loop and you copy the vectors in to new ones in the order you need them.
I do not think there is a need for an example.

Sorry to revive an old thread but did this item on your to-do list ever get done?

[quote=“couet”]You do a simple “for” loop and you copy the vectors in to new ones in the order you need them.
I do not think there is a need for an example.[/quote]

Also, in an X-Y graph this does not mirror the plot in the Y-axis as you are simply defining a set of points, the order in which this is done makes no difference.

The desired effect is obtained by multiplying all X values by -1, but then the axis labels are wrong. It is not impossible to implement in code, but it is not trivial. Has there been any progress on this?

The status is still the same.

Still the same with reversing axes ? :slight_smile:

This can be done using this example.
reversegraphXaxis.C (1.3 KB)

Would it be possible to have this as a native feature in the ROOT core (by adding a new method in TGraph and others for example)?

Done. Two new options are available in the ROOT master to draw TGraph with reverse axis.

2 Likes