# Plot polar TH2

**URL:** <https://root-forum.cern.ch/t/plot-polar-th2/9476>\
**Category:** ROOT\
**Created:** [January 14, 2010, 11:10pm UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476 "2010-01-14T23:10:27Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![zhiyiliu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/85e7bf/32.png) [@zhiyiliu](https://root-forum.cern.ch/u/zhiyiliu)\
**Post date:** [January 14, 2010, 11:10pm UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/1 "2010-01-14T23:10:27Z")

</div>

Hi,  
I am trying to draw a TH2D plot in a polar coordinate. I have tried:

tree-\>Draw(“phi:theta\>\>h2”, “”, “pollego2z”);  
(its top view is the 1st plot attached)

and  
tree-\>Draw(“phi:theta\>\>h2”, “”, “polcolz”);  
(the 2nd plot attached)  
.

But both of them are not satisfied. Plot 1 needs to add axis and y-axis of plot 2 has a problem. I am wondering if I can plot a TH2D plot in a polar coordinate like :

[root.cern.ch/root/html/TGraphPolar.html](http://root.cern.ch/root/html/TGraphPolar.html)

.

Thank you,  
Zhiyi.

 ![](https://root-forum.cern.ch/uploads/default/original/2X/e/e77017a002b1fa8eedd972ecdce97aa5ad24213a.png)  
  
 ![](https://root-forum.cern.ch/uploads/default/original/2X/f/f11506750fc46ca1f066ac21f4c6de8b611da58b.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:** [January 15, 2010, 8:49am UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/2 "2010-01-15T08:49:19Z")

</div>

I will investigate. Surely some improved can be made.

---

<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:** [January 15, 2010, 9:15am UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/3 "2010-01-15T09:15:24Z")

</div>

I do not understand how you get the first plot. In my case I get 2 axis when I use POLCOLZ. See the attached picture. I am using the latest ROOT version.

 ![](https://root-forum.cern.ch/uploads/default/original/2X/5/53ec24a45e25552c7aa1be4fa1e3bdc9b0fd7239.gif)

---

<div class="post-metadata">

**Author:** ![zhiyiliu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/85e7bf/32.png) [@zhiyiliu](https://root-forum.cern.ch/u/zhiyiliu)\
**Post date:** [January 16, 2010, 8:21am UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/4 "2010-01-16T08:21:11Z")

</div>

Thanks for your testing.

However, in your plot, x and y axises are not meaningful for a polar coordinate.

---

<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:** [January 18, 2010, 1:43pm UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/5 "2010-01-18T13:43:39Z")

</div>

Yes the angle is missing. That’s known . It has to be implemented.  
I was just pointing out the fact that there is some axis.

---

<div class="post-metadata">

**Author:** ![zhiyiliu](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/85e7bf/32.png) [@zhiyiliu](https://root-forum.cern.ch/u/zhiyiliu)\
**Post date:** [February 9, 2011, 10:26pm UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/6 "2011-02-09T22:26:41Z")

</div>

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.

```auto
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",
                                             ymin, ymax,
                                             0, 2*TMath::Pi());
  gp->SetNdivRadial(4);
  gp->SetLineColor(17);
  gp->Draw();
  can->Update();
  
  TPaletteAxis *palette = (TPaletteAxis*)h2d->GetListOfFunctions()->FindObject("palette");
  palette->SetX1NDC(0.89);
  palette->SetX2NDC(0.92);
  palette->SetY1NDC(0.6);
  palette->SetY2NDC(0.97);
  palette->SetLabelSize(0.03);
  can->Modified();
  
  return can; 
}
```

> [@couet](#):
>
> Yes the angle is missing. That’s known . It has to be be implemented. I was just pointing out the fact that there is some axis.

---

<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:** [February 14, 2011, 3:51pm UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/7 "2011-02-14T15:51:23Z")

</div>

Thanks for the input.

---

<div class="post-metadata">

**Author:** ![adavid](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/ecb155/32.png) [@adavid](https://root-forum.cern.ch/u/adavid)\
**Post date:** [May 22, 2013, 12:01pm UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/8 "2013-05-22T12:01:31Z")

</div>

Hi,

Sorry for reopening this thread, but I am unable to get a 2D polar plot going. Basically I can get:

```auto
Data->Draw("lead_E:atan(lead_px/lead_py)","","SURF7 POL")
```

 ![](https://root-forum.cern.ch/uploads/default/original/2X/5/5da2b5af2f37e5a09955d21f870cfabb91634d5d.png)  

Naively I thought I should only do

```auto
Data->Draw("lead_E:atan(lead_px/lead_py)","","COL POL")
```

 ![](https://root-forum.cern.ch/uploads/default/original/2X/3/3629d8dabf90002d667baf403e1ccc72ca9423c7.png)  

Which looks nothing near to what the SURF managed to do.  
In short, how can I just get the 2D polar plot from a TH2?

Thanks,

Andre

ps - I am on

```auto
ROOT 5.34/05 (tags/v5-34-05@48582, Feb 14 2013, 23:27:57 on macosx64)
```

---

<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:** [May 22, 2013, 12:15pm UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/9 "2013-05-22T12:15:19Z")

</div>

I works for me with 5.34 on mac

```auto
root [1] ntuple->Draw("px:py","","colz pol")
```

 ![](https://root-forum.cern.ch/uploads/default/original/2X/e/e5364e58ce1c0c21c5504b1a8cdc711f56aae841.png)

---

<div class="post-metadata">

**Author:** ![londumas](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/6de8d8/32.png) [@londumas](https://root-forum.cern.ch/u/londumas)\
**Post date:** [June 5, 2014, 7:49am UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/10 "2014-06-05T07:49:47Z")

</div>

Hello @zhiyiliu,

Can you just explain how your TH2D has to be build before your function ?  
Is it :

- TH2D\* th2 = new TH2D(“th2”," name",binTheta, thetaMin, thetaMax, binR, rMin, rMax);  
or
- TH2D\* th2 = new TH2D(“th2”," name",binX, xMin, xMax, binY, yMin, yMax);  
?

Because I would like to use the first one, but I don’t manage to do so with “Draw(pol colz);” or your function. Also do you now if theta has to be in degree or radian ? The documentation don’t say.

Thank you for your help.

---

<div class="post-metadata">

**Author:** ![ebutler](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@ebutler](https://root-forum.cern.ch/u/ebutler)\
**Post date:** [July 1, 2014, 11:55am UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/11 "2014-07-01T11:55:37Z")

</div>

I had some success trawling through the source code to understand what’s going on. It seems that the axis ranges are messed up if when drawing a TH2 with Draw(“COLZ POL”). I managed to get it working by drawing a dummy histogram beforehand.

[code]void PolarTH2()  
{  
gStyle-\>SetOptStat(0);

```
Double_t rmax(1.);

TH2D* pol_his = new TH2D("polarHist", "polarHist", 20, 0., 2.*TMath::Pi(), 20, 0., rmax); // the polar data. X maps to theta, Y maps to R

Double_t theta, r;
//fill the histogram with something nice.
for(Int_t i=1; i<=pol_his->GetNbinsX(); i++)
{
	theta = pol_his->GetXaxis()->GetBinCenter(i);
	for(Int_t j=1; j<=pol_his->GetNbinsY(); j++)
	{
		r = pol_his->GetYaxis()->GetBinCenter(j);
		pol_his->SetBinContent(i, j, r*cos(2.*theta) );
	}
}

//make a dummy histogram to set the axis range from -rmax to rmax in both directions
TH2D* dummy_his = new TH2D("dummy", "histo title", 100, -rmax, rmax, 100, -rmax, rmax);

TCanvas* c1 = new TCanvas("theCanvas", "theCanvas", 600, 600);
dummy_his->Draw("COL"); // draw the dummy histogram first
pol_his->Draw("COL POL SAME"); // now draw the data histogram. If it has "SAME" it will use the first histogram ranges

```

}[/code]

 ![](https://root-forum.cern.ch/uploads/default/original/2X/7/7f20725ddcf82d4a694f669e040f4fd7193cd253.png)  

[PolarTH2.cpp](https://root-forum.cern.ch/uploads/default/original/2X/2/217cc8aa13dfdb96fbc9f3f62d22822c2c9e6e50.cpp) (940 Bytes)

---

<div class="post-metadata">

**Author:** ![ksmith](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ksmith/32/12567_2.png) [@ksmith](https://root-forum.cern.ch/u/ksmith)\
**Post date:** [August 14, 2015, 6:52pm UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/12 "2015-08-14T18:52:11Z")

</div>

Any status on improving the POL draw commands? It is very awkward to have to make a blank histograms just to create the proper histogram axis.

Thanks.

---

<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:** [August 17, 2015, 7:09am UTC](https://root-forum.cern.ch/t/plot-polar-th2/9476/13 "2015-08-17T07:09:32Z")

</div>

May be you ca post a bug report on JIRA. It is easier to follow.
