# Polar Color Graph

**URL:** <https://root-forum.cern.ch/t/polar-color-graph/29629>\
**Category:** ROOT\
**Created:** [July 3, 2018, 6:33pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629 "2018-07-03T18:33:24Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 3, 2018, 6:33pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/1 "2018-07-03T18:33:24Z")

</div>

* * *

\_ROOT Version:Both 5 and 6  
\_Platform:Ubuntu 14-16-18  
_Compiler:_ Not Provided

* * *

Dear root experts,

I would like to make a polar graph which I don’t know if possible under root framework.

I just made a quick sketch of it and attached.

I will define histograms like

TGraphErrors\* hist[4];

hist[i] = new TGraphErrors(16, x, y, ex, ey); for i=0,1

where double x[16]; and double y[16];

hist[j] = new TGraphErrors(8, x, y, ex, ey); for j=2,3

where double x[8]; and double y[8];

Polar graph will have 4 concentric circles and 16/8 division along the circle. Divisions will be labeled by numbers and not by angles.

I tried TGraphPolar instead of TGraphErrors, couldn’t do.

I tried TH2D and then fill x,y and then Draw-\>(“colzpol”) or Draw-\>(“pollego2z”)

I tried lots of other stuff too, all to no avail.

Is it possible to make a graph as shown in the sketch ? If possible, can you tell me how I can do it ? Just give me the recipe, I can do the rest.

Thanks

 ![2018%2020%2053%2057](https://root-forum.cern.ch/uploads/default/original/3X/1/a/1ad451d8b7fa01c4fff6edc695a29fb35020578d.jpg)

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [July 4, 2018, 7:15am UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/2 "2018-07-04T07:15:49Z")

</div>

Hi,

I’m sure @couet knows if it’s possible, and how 😉

Cheers, Bertrand.

---

<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:** [July 4, 2018, 1:34pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/3 "2018-07-04T13:34:09Z")

</div>

may be this:

```auto
{
   TCanvas *c1 = new TCanvas("c1","c1",600,400);
   TH2F *hcol1 = new TH2F("hcol1","Option COLor combined with POL",40,-4,4,40,-4,4);
   Float_t px, py;
   for (Int_t i = 0; i < 25000; i++) {
      gRandom->Rannor(px,py);
      hcol1->Fill(px,py);
   }
   hcol1->Draw("COLZPOL");
   return c1;
}

```

---

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 4, 2018, 2:45pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/4 "2018-07-04T14:45:38Z")

</div>

I tried that one indeed. But it doesn’t produce plots close to what I want.

For a single circle, it produces a plot of which top view is attached ![polarcolor_1_1_20_2](https://root-forum.cern.ch/uploads/default/original/3X/1/0/102c8b38f9cb38544afa6de63df6e3ab8e14c22b.gif)

So, what you see is, x value is represented by the number of divisions in the phi plane, which is what I want, but y value is represented by the radius, and not by the color.

I wish that y value is not represented by radius, all the y values will have the same radius but differ only in color corresponding to their value.

Radius will be assigned to different circles.

Closest to what I demand is done only by one person, zhiyi, in this topic. Maybe I should have a contact with him. [Plot polar TH2](https://root-forum.cern.ch/t/plot-polar-th2/9476)

---

<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:** [July 4, 2018, 3:01pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/5 "2018-07-04T15:01:33Z")

</div>

can you post a small script ?

---

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 4, 2018, 4:11pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/6 "2018-07-04T16:11:10Z")

</div>

I created a small macro,

```auto
void polarcolor(){
        TCanvas * c1 = new TCanvas("c1", "", 600, 600);
        c1->SetTheta(90);
        c1->SetPhi(180);
        TH2D* hist[2];

        hist[0] = new TH2D("polar hist","",8, 0 , 8, 10, 0.8 , 1.0);
        hist[1] = new TH2D("polar hist","",4, 0 , 8, 10, 0.8 , 1.0);

        double x1[8]={1,2,3,4,5,6,7,8};
        double y1[8]={0.85,0.9,0.95,0.95,0.98,0.86,0.82,0.94};
        double x2[4]={1,3,5,7};
        double y2[4]={0.92,0.95,0.88,0.99};

        for(int i=0; i<8; i++){hist[0]->Fill(x1[i],y1[i]);}
        for(int j=0; j<4; j++){hist[1]->Fill(x2[j],y2[j]);}

        for(int k=0; k<2; k++){
                c1->cd(1);
                if(k==0){hist[k]->Draw("pollego2z");}
                else{hist[k]->Draw("same");}
        }
}

```

If we succeed with this small example a plot like the following, then we are done. I tried polcolz instead of pollego2z, too. Didn’t work either.

(note : colors are not representative of the data in the macro above)

 ![2018%2019%2001%2055](https://root-forum.cern.ch/uploads/default/original/3X/c/2/c249c578412b7664a476980758b831edcff1f0c5.jpg)

---

<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:** [July 4, 2018, 4:47pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/7 "2018-07-04T16:47:54Z")

</div>

May be this:

```auto
void polarcolor(){
   TCanvas * c1 = new TCanvas("c1", "", 600, 600);
   c1->SetTheta(90.);
   c1->SetPhi(0.);

   TH2D* hist[2];
   auto hs = new THStack();

   hist[0] = new TH2D("polar hist","",8, 0 , 8, 10, 0.8 , 1.0);
   hist[1] = new TH2D("polar hist","",4, 0 , 8, 10, 0.8 , 1.0);

   double x1[8]={1,2,3,4,5,6,7,8};
   double y1[8]={0.85,0.9,0.95,0.95,0.98,0.86,0.82,0.94};
   double x2[4]={1,3,5,7};
   double y2[4]={0.92,0.95,0.88,0.99};

   for(int i=0; i<8; i++){hist[0]->Fill(x1[i],y1[i]);}
   for(int j=0; j<4; j++){hist[1]->Fill(x2[j],y2[j]);}

   hist[0]->SetFillColor(kRed);
   hist[1]->SetFillColor(kBlue);

   hs->Add(hist[0]);
   hs->Add(hist[1]);

   hs->Draw("lego1 0 pol ");

}

```

 ![58](https://root-forum.cern.ch/uploads/default/original/3X/8/6/86975ed0aaac8c7fb9ae564e23005ef3c831d178.png)

---

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 4, 2018, 5:16pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/8 "2018-07-04T17:16:02Z")

</div>

Not actually, @couet,

The red ones must have the same radial distance from the center and not all of them must have the same color. The same is true for blue ones. The color must represent y value.

So, I thought a 3D histogram might be the answer, where, x value will be the number of divisions along the phi plane, y value will be radial distance and z value will be the value we want to show with color.

```auto
        hist[0] = new TH3D("polar hist","", 8, 0 , 8, 10, 0, 10, 10, 0.8 , 1.0);
        hist[1] = new TH3D("polar hist","", 4, 0 , 8, 10, 0, 10, 10, 0.8 , 1.0);

        double x1[8]={1,2,3,4,5,6,7,8};
        double y1[8]={5,5,5,5,5,5,5,5};
        double z1[8]={0.85,0.9,0.95,0.95,0.93,0.86,0.83,0.94};

        double x2[4]={1,3,5,7};
        double y2[4]={6,6,6,6};
        double z2[4]={0.92,0.95,0.88,0.99};

```

However, it didn’t work. Also THStack is not defined for 3D histograms.

---

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 5, 2018, 1:57am UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/9 "2018-07-05T01:57:28Z")

</div>

@couet, I managed to plot 2D cartesian version. It just looks fine. All I need is the polar version of this plot with appropriate labeling of polar divisions. When I change the draw option to polcolz, root gets confused all of a sudden.

```auto
void polarcolor(){
        TCanvas * c1 = new TCanvas("c1", "", 600, 600);

        gStyle->SetOptStat(0);
        gStyle->SetPalette(kDeepSea);

        TH2D* hist[2];

        hist[0] = new TH2D("polar hist","",10, 0,10,4,0,4);
        hist[1] = new TH2D("polar hist","",6, -1,11,4,0,4);

        double x1[8]={1,2,3,4,5,6,7,8};
        double y1[8]={0.85,0.95,0.85,0.95,0.85,0.95,0.85,0.95};
        double x2[4]={1,3,5,7};
        double y2[4]={0.95,0.85,0.95,0.85};

        for(int i=0; i<8; i++){hist[0]->Fill(x1[i],1,y1[i]);}
        for(int j=0; j<4; j++){hist[1]->Fill(x2[j],2,y2[j]);}

        c1->cd(1);
        hist[0]->Draw("colz");
        hist[1]->Draw("colsame");
}

```

 ![2Dcolorplot](https://root-forum.cern.ch/uploads/default/original/3X/8/8/8823a9cec4b150b735d151358e4f60f10b55c44e.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:** [July 5, 2018, 8:40am UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/10 "2018-07-05T08:40:27Z")

</div>

I think with the current version (0,0) is assumed to be in the center of the plot when you use the COLPOL option. So what you are trying to do will not work. The following code shows the problem:

```auto
void polarcolor(){
        TCanvas * c1 = new TCanvas("c1", "", 600, 600);

        gStyle->SetOptStat(0);

        TH2D* hist[2];

        hist[0] = new TH2D("h1","h1",10, 0,10,4,0,4);
        hist[1] = new TH2D("h2","h2",6, -1,11,4,0,4);

        double x1[8]={1,2,3,4,5,6,7,8};
        double y1[8]={0.85,0.95,0.85,0.95,0.85,0.95,0.85,0.95};
        double x2[4]={1,3,5,7};
        double y2[4]={0.95,0.85,0.95,0.85};

        for(int i=0; i<8; i++){hist[0]->Fill(x1[i],1,y1[i]);}
        for(int j=0; j<4; j++){hist[1]->Fill(x2[j],2,y2[j]);}

        hist[0]->Draw("colz pol");
}

```

---

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 5, 2018, 1:45pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/11 "2018-07-05T13:45:16Z")

</div>

@couet, What do you get as the output of this code ?

I am using root v6.10.02 and I get the following which is not what I want. The center of the circle must be at the center of the pad so that the whole of the circle is seen.

I don’t know how pol is mapping from cartesian coordinates but it does it in an awkward way.

 ![polarcolor_trial](https://root-forum.cern.ch/uploads/default/original/3X/f/5/f550a435d446f7c50ff19873b6cfbb3a77e422f7.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:** [July 5, 2018, 2:09pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/12 "2018-07-05T14:09:02Z")

</div>

> [@querious](#):
>
> What do you get as the output of this code ?

I get the same plot you posted that’s how I conclude the polar plot is drawn according to (0,0)

---

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 5, 2018, 2:30pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/13 "2018-07-05T14:30:00Z")

</div>

@couet, Playing with the histograms ranges which are initially set suitable for cartesian system, I am able to make a progress to some degree, but still not in there,

So, this one produces the following,

```auto
void polarcolor(){
        TCanvas * c1 = new TCanvas("c1", "", 600, 600);
     
        gStyle->SetOptStat(0);
        gStyle->SetPalette(kDeepSea);

        TH2D* hist[2];

        hist[0] = new TH2D("h1","h1",16, -TMath::Pi()*2 , TMath::Pi()*2, 8, -4 , 4);
        hist[1] = new TH2D("h2","h2", 8, -TMath::Pi()*2 , TMath::Pi()*2, 8, -4 , 4);

        double x1[8]={1,2,3,4,5,6,7,8};
        double y1[8]={0.75,0.95,0.85,0.95,0.75,0.85,0.95,0.85};
        double x2[4]={1,3,5,7};
        double y2[4]={0.75,0.85,0.75,0.95};

        for(int i=0; i<8; i++){hist[0]->Fill(x1[i],1,y1[i]);}
        for(int j=0; j<4; j++){hist[1]->Fill(x2[j],2,y2[j]);}

        hist[0]->Draw("pol colz");
        hist[1]->Draw("pol col same");
}

```

 ![polarcolor_trial3](https://root-forum.cern.ch/uploads/default/original/3X/f/7/f7fdada94527c2bd9ae76ee9a91ee113d2c1434c.png)

Do you have an idea why some portions are not drawn, @couet ?

---

<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:** [July 5, 2018, 2:43pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/14 "2018-07-05T14:43:44Z")

</div>

As explained [here](https://root.cern/doc/master/classTHistPainter.html#HP14) empty bin are not drawn unless the minimum of the histogram is negative

---

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 8, 2018, 12:58pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/15 "2018-07-08T12:58:56Z")

</div>

@couet I would like to report an issue, Olivier. When I set the histogram range as (0,2π) both along x and y axes with appropriate binning, I get the the figure on the left, which is perfectly good, y values are correctly represented. Then, I reset the histogram range as (-2π,2π) for both axes, doubling the binning as well. I do this, because when drawing this one with “pol” option, it will give me the desired plot. However, this time, hist[0] is not correctly filled in, as you can see in the figure on the right. The range of color palette doubled as well, which should not be the case. On the other hand, hist[1] still seems ok. What’s wrong here ? I am attaching the macros for both figures. Only lines 11-12 differ.

 ![issue](https://root-forum.cern.ch/uploads/default/original/3X/2/5/259349e8158c6b64b213b36c334ef88e69d6fd15.png)[polarcolor\_v17.C](https://root-forum.cern.ch/uploads/default/original/3X/a/7/a772b1a19fb8b9e39f93040d862dc956b0dd2657.C) (924 Bytes)  
[polarcolor\_v19.C](https://root-forum.cern.ch/uploads/default/original/3X/7/4/74bf16afbfe98178a1cd388d8accddf92482b1aa.C) (872 Bytes)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 8, 2018, 1:34pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/16 "2018-07-08T13:34:54Z")

</div>

You are facing floating point “rounding errors” (coming mainly from bins’ edges calculation in ROOT).  
To understand it better, try to calculate e.g.:

```auto
((TMath::Pi()*5.)/3.) - ((TMath::Pi()/3.)*5.)

```

and try your macro with e.g.:

```auto
double slip = -1e-11; // try with -1e-11, 0, +1e-11
hist[0] = new TH2D("h1","h1",16, -TMath::TwoPi(), TMath::TwoPi() + slip, 8, -TMath::TwoPi(), TMath::TwoPi() + slip);
hist[1] = new TH2D("h2","h2", 8, -TMath::TwoPi(), TMath::TwoPi() + slip, 8, -TMath::TwoPi(), TMath::TwoPi() + slip);

```

---

<div class="post-metadata">

**Author:** ![querious](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/q/c4cdca/32.png) [@querious](https://root-forum.cern.ch/u/querious)\
**Post date:** [July 8, 2018, 1:58pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/17 "2018-07-08T13:58:42Z")

</div>

Exactly !!! @Wile_E_Coyote  
The calculation turned out to be a non-zero number  
Now with an infinitessimal slip, I can get the following figure which is what I wanted from the very beginning. y values seem to be correctly color-mapped.

Don’t know words to express the magnitude of my gratitude, Wile\_E\_Coyote.

 ![polarcolor4](https://root-forum.cern.ch/uploads/default/original/3X/c/e/ce8cbe3110406d86cfa703b72eb58260bf9eff2e.png)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 8, 2018, 2:08pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/18 "2018-07-08T14:08:28Z")

</div>

Note that you probably need appropriate “slips” (positive or negative, I’m not sure, you need to play with it) to the negative “[xy]low” of your histograms (I’ve modified the positive “[xy]up” only), too.

---

<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:** [July 22, 2018, 2:08pm UTC](https://root-forum.cern.ch/t/polar-color-graph/29629/19 "2018-07-22T14:08:28Z")

</div>

Closed on July 22, 2018, 2:08pm UTC

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