Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist021_TH2_reverse_axis.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// \preview Example showing an histogram with reverse axis.
5///
6/// \macro_image
7/// \macro_code
8///
9/// \date July 2016
10/// \author Olivier Couet
11
12void ReverseXAxis(TH1 *h);
13void ReverseYAxis(TH1 *h);
14
15void hist021_TH2_reverse_axis()
16{
17 TH2F *hpxpy = new TH2F("hpxpy", "py vs px", 40, -4, 4, 40, -4, 4);
18 Float_t px, py;
19 TRandom r;
20 for (Int_t i = 0; i < 25000; i++) {
21 r.Rannor(px, py);
22 hpxpy->Fill(px, py);
23 }
24 TCanvas *c1 = new TCanvas("c1");
25 hpxpy->Draw("colz");
26 ReverseXAxis(hpxpy);
27 ReverseYAxis(hpxpy);
28}
29
30void ReverseXAxis(TH1 *h)
31{
32 // Remove the current axis
33 h->GetXaxis()->SetLabelOffset(999);
34 h->GetXaxis()->SetTickLength(0);
35
36 // Redraw the new axis
37 gPad->Update();
38 TGaxis *newaxis = new TGaxis(gPad->GetUxmax(), gPad->GetUymin(), gPad->GetUxmin(), gPad->GetUymin(),
39 h->GetXaxis()->GetXmin(), h->GetXaxis()->GetXmax(), 510, "-");
40 newaxis->SetLabelOffset(-0.03);
41 newaxis->Draw();
42}
43
44void ReverseYAxis(TH1 *h)
45{
46 // Remove the current axis
47 h->GetYaxis()->SetLabelOffset(999);
48 h->GetYaxis()->SetTickLength(0);
49
50 // Redraw the new axis
51 gPad->Update();
52 TGaxis *newaxis = new TGaxis(gPad->GetUxmin(), gPad->GetUymax(), gPad->GetUxmin() - 0.001, gPad->GetUymin(),
53 h->GetYaxis()->GetXmin(), h->GetYaxis()->GetXmax(), 510, "+");
54 newaxis->SetLabelOffset(-0.03);
55 newaxis->Draw();
56}
ROOT::R::TRInterface & r
Definition Object.C:4
#define h(i)
Definition RSha256.hxx:106
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
float Float_t
Float 4 bytes (float).
Definition RtypesCore.h:71
#define gPad
The Canvas class.
Definition TCanvas.h:23
void SetLabelOffset(Float_t labeloffset)
Definition TGaxis.h:108
TH1 is the base class of all histogram classes in ROOT.
Definition TH1.h:109
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3097
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:363
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:293
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
return c1
Definition legend1.C:41