Hello,
I would like to invert the x axis without changing anything else in the TGraph.
How could I do?
thanks!
There is no simple way right now without changing the graph. The following macro gives an example (on the Y axis).
reversegraphaxis ()
{
const Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i*0.1+5;
y[i] = 10*sin(x[i]+0.2);
}
g = new TGraph(n,x,y);
g->SetMarkerStyle(21);
g->Draw("APL");
ReverseYAxis(g);
ReverseYGraph(g);
}
ReverseYAxis (TGraph *g)
{
// Remove the current axis
g->GetYaxis()->SetLabelOffset(999);
g->GetYaxis()->SetTickLength(0);
// Redraw the new axis
gPad->Update();
TGaxis *newaxis = new TGaxis(gPad->GetUxmin(),
gPad->GetUymax(),
gPad->GetUxmin()-0.001,
gPad->GetUymin(),
g->GetYaxis()->GetXmin(),
g->GetYaxis()->GetXmax(),
510,"+");
newaxis->SetLabelOffset(-0.04);
newaxis->Draw();
}
ReverseYGraph (TGraph *g)
{
// Create a new graph
Int_t n = g->GetN();
Double_t *x = g->GetX();
Double_t *y = g->GetY();
Double_t yr[100];
Double_t dy = g->GetYaxis()->GetXmin()+g->GetYaxis()->GetXmax();
for (Int_t i=0; i<n; i++) {
yr[i] = -y[i]+dy;
}
gr = new TGraph(n,x,yr);
gr->SetMarkerStyle(20);
gr->SetLineColor(kRed);
gr->SetMarkerColor(kRed);
gr->Draw("PL");
}
thanks!
But how do I change it for the Xaxis? I tried but It seems not to work.
reversegraphXaxis ()
{
const Int_t n = 20;
Double_t x[n], y[n];
for (Int_t i=0;i<n;i++) {
x[i] = i*0.1+5;
y[i] = 10*sin(x[i]+0.2);
}
g = new TGraph(n,x,y);
g->SetMarkerStyle(21);
g->Draw("APL");
ReverseXAxis(g);
ReverseXGraph(g);
}
ReverseXAxis (TGraph *g)
{
// 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,"-SDH");
newaxis->SetLabelOffset(-0.03);
newaxis->Draw();
}
ReverseXGraph (TGraph *g)
{
// Create a new graph
Int_t n = g->GetN();
Double_t *x = g->GetX();
Double_t *y = g->GetY();
Double_t xr[100];
Double_t dx = g->GetXaxis()->GetXmin()+g->GetXaxis()->GetXmax();
for (Int_t i=0; i<n; i++) {
xr[i] = -x[i]+dx;
}
gr = new TGraph(n,xr,y);
gr->SetMarkerStyle(20);
gr->SetLineColor(kRed);
gr->SetMarkerColor(kRed);
gr->Draw("PL");
}