# Polar Axis For Histogram

**URL:** <https://root-forum.cern.ch/t/polar-axis-for-histogram/45194>\
**Category:** ROOT\
**Tags:** graphics\
**Created:** [June 1, 2021, 3:22pm UTC](https://root-forum.cern.ch/t/polar-axis-for-histogram/45194 "2021-06-01T15:22:59Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mperrint](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mperrint](https://root-forum.cern.ch/u/mperrint)\
**Post date:** [June 1, 2021, 3:22pm UTC](https://root-forum.cern.ch/t/polar-axis-for-histogram/45194/1 "2021-06-01T15:22:59Z")

</div>

Hello,

I am trying to draw a TH2D as a polar plot and I am not able to have axes overlaid on it.  
I used this trick:

> [@Plot polar TH2](https://root-forum.cern.ch/t/plot-polar-th2/9476/6):
>
> I do not know if it has been implemented yet. I worked out a solution for this long time ago and would post it here, may be helpful for other guys. TCanvas \* plot\_histogram\_in\_polar(TH2D \* h2d) { h2d-\>SetStats(false); TCanvas \* can = new TCanvas("can", "", 600, 600); can-\>SetTheta(90); can-\>SetPhi(180); h2d-\>Draw("pollego2z"); double ymax = h2d-\>GetYaxis()-\>GetXmax(); double ymin = h2d-\>GetYaxis()-\>GetXmin(); TGraphPolargram \* gp = new TGraphPolargram("g", …

It works, but as soon as I change the canvas’ margin to properly show the palette, the TGraphPolarGram is not rescaled… see below

 ![RatesP10Polar](https://root-forum.cern.ch/uploads/default/original/3X/9/a/9a2125b047fc5e95a1ec0fe3b0b845bf60131de5.png)

any idea how to scale it properly or display the same axes in a better way?

Thanks in advance for your help

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 1, 2021, 3:29pm UTC](https://root-forum.cern.ch/t/polar-axis-for-histogram/45194/2 "2021-06-01T15:29:23Z")

</div>

You should keep the Pad square

---

<div class="post-metadata">

**Author:** ![mperrint](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mperrint](https://root-forum.cern.ch/u/mperrint)\
**Post date:** [June 1, 2021, 4:13pm UTC](https://root-forum.cern.ch/t/polar-axis-for-histogram/45194/3 "2021-06-01T16:13:05Z")

</div>

With a rectangular canvas it works see attached. The problem arises when I do cv.SetRightMargin(0.15).  
I added attached also a macro to reproduce the problem.  
[Test.C](https://root-forum.cern.ch/uploads/short-url/dNI5Oq6NU0qOZoyFUBoJbeFvDTj.C) (3.8 KB)

 ![RatesP25Polar](https://root-forum.cern.ch/uploads/default/original/3X/9/1/91f702bb57093258fdc87730f47d6d5f50497930.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 1, 2021, 4:57pm UTC](https://root-forum.cern.ch/t/polar-axis-for-histogram/45194/4 "2021-06-01T16:57:09Z")

</div>

I made a simpler version of your macro to ease the debugging:

```auto
void mperrint() {

   double p0 = 25;
   const char* fileOut = "bla.pdf";

   gStyle->SetLabelSize(0.06,"xyz");
   gStyle->SetLabelOffset(0.012,"xy");
   gStyle->SetTitleOffset(1.05,"x");
   gStyle->SetTitleOffset(1.15,"y");
   gStyle->SetTitleSize(0.065,"xy");
   gStyle->SetTitleBorderSize(0);
   gStyle->SetOptStat("");

   TLorentzVector* p4Pi = new TLorentzVector();
   double mPi = 0.13957;
   p4Pi->SetXYZM(0,0,p0,mPi);

   double binW = 200/2595.;
   double thMax =1.5;
   auto hPol = new TH2D("hPol", "",40,-TMath::Pi(),TMath::Pi(),90,0,thMax);
   auto hTh = new TH1D("hTh" , ";#theta_{#nu#pi} [#circ]; evts per sr",90,0,thMax);

   hPol->GetZaxis()->SetMaxDigits(3);
   hTh->GetYaxis()->SetMaxDigits(3);

   double bW = hTh->GetBinWidth(2)*TMath::DegToRad();
   double bWPhi = 2*TMath::Pi()/40;

   auto cv = new TCanvas();
   double mMu = 0.10565837;
   double massDecay[2] = {mMu,0.};
   TGenPhaseSpace generator;

   int nP=100;

   for (int ip(0); ip < nP; ip++) {
      int nPh = int(gRandom->Poisson(1000.));
      p4Pi->SetXYZM(0,0,p0,mPi);
      generator.SetDecay(*p4Pi, 2, massDecay);

      for (int i(0); i<nPh ; i++) {
         // generate
         Double_t weight = generator.Generate();

         // true
         TLorentzVector* p4Mu = generator.GetDecay(0);
         TLorentzVector* p4Nu = generator.GetDecay(1);
         double th = p4Nu->Vect().Theta();
         double phi = p4Nu->Vect().Phi();

         hPol->Fill(phi,th*TMath::RadToDeg(),weight/nP / bW / bWPhi);
         //hPol.Fill(phi,th*TMath::RadToDeg(),weight/nP);
         hTh->Fill(th*TMath::RadToDeg() ,weight/nP / bW / (2*TMath::Pi()));
      }
   }

  hPol->Draw("lego2 polz");

  hPol->SetLineColorAlpha(kBlack, 0.0);
  cv->SetGrid(0,0);
  cv->SetRightMargin(0.15);
  cv->SetTheta(90.);
  cv->SetPhi(0.);
  gPad->Update();

  auto gp = new TGraphPolargram("g",0,thMax, -180., 180.);
  gp->SetToDegree ();
  gp->SetNdivPolar(8);
  gp->SetNdivRadial(4);
  gp->SetLineColor(1);
  gp->Draw();
}

```

This Marco super-impose a 3D pol and a 2D polargram. Superimposing is not straightforward in that case. I am looking for a solution with an extra pad…

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 2, 2021, 7:28am UTC](https://root-forum.cern.ch/t/polar-axis-for-histogram/45194/5 "2021-06-02T07:28:24Z")

</div>

I found a way…

```auto
void mperrint() {

   double p0 = 25;
   const char* fileOut = "bla.pdf";

   gStyle->SetLabelSize(0.06,"xyz");
   gStyle->SetLabelOffset(0.012,"xy");
   gStyle->SetTitleOffset(1.05,"x");
   gStyle->SetTitleOffset(1.15,"y");
   gStyle->SetTitleSize(0.065,"xy");
   gStyle->SetTitleBorderSize(0);
   gStyle->SetOptStat("");

   TLorentzVector* p4Pi = new TLorentzVector();
   double mPi = 0.13957;
   p4Pi->SetXYZM(0,0,p0,mPi);

   double binW = 200/2595.;
   double thMax =1.5;
   auto hPol = new TH2D("hPol", "",40,-TMath::Pi(),TMath::Pi(),90,0,thMax);
   auto hTh = new TH1D("hTh" , ";#theta_{#nu#pi} [#circ]; evts per sr",90,0,thMax);

   hPol->GetZaxis()->SetMaxDigits(3);
   hTh->GetYaxis()->SetMaxDigits(3);

   double bW = hTh->GetBinWidth(2)*TMath::DegToRad();
   double bWPhi = 2*TMath::Pi()/40;

   auto cv = new TCanvas();
   double mMu = 0.10565837;
   double massDecay[2] = {mMu,0.};
   TGenPhaseSpace generator;

   int nP=100;

   for (int ip(0); ip < nP; ip++) {
      int nPh = int(gRandom->Poisson(1000.));
      p4Pi->SetXYZM(0,0,p0,mPi);
      generator.SetDecay(*p4Pi, 2, massDecay);

      for (int i(0); i<nPh ; i++) {
         // generate
         Double_t weight = generator.Generate();

         // true
         TLorentzVector* p4Mu = generator.GetDecay(0);
         TLorentzVector* p4Nu = generator.GetDecay(1);
         double th = p4Nu->Vect().Theta();
         double phi = p4Nu->Vect().Phi();

         hPol->Fill(phi,th*TMath::RadToDeg(),weight/nP / bW / bWPhi);
         //hPol.Fill(phi,th*TMath::RadToDeg(),weight/nP);
         hTh->Fill(th*TMath::RadToDeg() ,weight/nP / bW / (2*TMath::Pi()));
      }
   }

   hPol->Draw("lego2 polz");

   hPol->SetLineColorAlpha(kBlack, 0.0);
   cv->SetRightMargin(0.15);
   cv->SetTheta(90.);
   cv->SetPhi(0.);
   gPad->Update();

   auto PPad = new TPad("p","p",0.01,0.,.94,1.);
   PPad->SetFillStyle(4000);
   PPad->Draw();
   PPad->cd();
   auto gp = new TGraphPolargram("g",0,thMax, -180., 180.);
   gp->SetToDegree ();
   gp->SetNdivPolar(8);
   gp->SetNdivRadial(4);
   gp->SetLineColor(1);
   gp->Draw();
}

```

 ![Screenshot 2021-06-02 at 09.28.07](https://root-forum.cern.ch/uploads/default/original/3X/7/d/7dee975753d885c9479ad8c568df46e174976654.jpeg)

---

<div class="post-metadata">

**Author:** ![mperrint](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/b9bd4f/32.png) [@mperrint](https://root-forum.cern.ch/u/mperrint)\
**Post date:** [June 2, 2021, 1:41pm UTC](https://root-forum.cern.ch/t/polar-axis-for-histogram/45194/6 "2021-06-02T13:41:03Z")

</div>

Thanks a lot! It works 🙂

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [June 16, 2021, 1:41pm UTC](https://root-forum.cern.ch/t/polar-axis-for-histogram/45194/7 "2021-06-16T13:41:31Z")

</div>

Closed on June 16, 2021, 1:41pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
