kshaw
June 17, 2008, 10:26am
1
Hi there
I have made a 2D COL plot. My problem is I want the X axis and the Y axis the other way around.
I am also making a 3D plot of the same thing. So I need to change the axis around only on the 2D plot. This means I cant change the input data.
Is there an option I can select to simply swap around axis on 2D plot?
Thanks
couet
June 17, 2008, 12:23pm
2
Here is an example of macro swapping axis. Note that if the number of bins is not the same along the X and Y axis the macro should be modified to take this case into account.
void swapaxis()
{
TCanvas *c = new TCanvas();
c->Divide(1,2);
TH2F *h1 = new TH2F("h1","h1",40,-4,4,40,-8,8);
TRandom r;
for (int i=0;i<20000;i++) h1->Fill(r.Gaus(),r.Gaus()+2);
c->cd(1); h1->Draw("col");
TH2F *h2 = swap_histogram_axis(h1);
c->cd(2); h2->Draw("col");
}
TH2F *swap_histogram_axis(TH2F *h1)
{
Int_t nbinx = h1->GetNbinsX();
Int_t nbiny = h1->GetNbinsY();
Double_t x1 = h1->GetXaxis()->GetBinLowEdge(1);
Double_t x2 = h1->GetXaxis()->GetBinLowEdge(nbinx)+h1->GetXaxis()->GetBinWidth(nbinx);
Double_t y1 = h1->GetYaxis()->GetBinLowEdge(1);
Double_t y2 = h1->GetYaxis()->GetBinLowEdge(nbiny)+h1->GetYaxis()->GetBinWidth(nbiny);
TH2F *h2 = new TH2F("h1_swap","h1_swap",nbiny,y1,y2,nbinx,x1,x2);
Double_t c;
for(int i=1; i<=nbinx; i++) {
for(int j=1; j<=nbiny; j++) {
c = h1->GetBinContent(i,j);
h2->SetBinContent(j,i,c);
}
}
return h2;
}
kshaw
June 17, 2008, 1:03pm
3
Thanks very much for your help.
The code works great!