Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
graphpolar.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphs
3 /// \notebook
4 /// Create and draw a polar graph.
5 ///
6 /// \macro_image
7 /// \macro_code
8 ///
9 /// \author Olivier Couet
10 
11 void graphpolar()
12 {
13  // Illustrates how to use TGraphPolar
14 
15  TCanvas * CPol = new TCanvas("CPol","TGraphPolar Examples",1200,600);
16  CPol->Divide(2,1);
17  CPol->cd(1);
18 
19  Double_t xmin=0;
20  Double_t xmax=TMath::Pi()*2;
21 
22  Double_t x[1000];
23  Double_t y[1000];
24  Double_t xval1[20];
25  Double_t yval1[20];
26 
27  TF1 * fplot = new TF1("fplot","cos(2*x)*cos(20*x)",xmin,xmax);
28 
29  for (Int_t ipt = 0; ipt < 1000; ipt++){
30  x[ipt] = ipt*(xmax-xmin)/1000+xmin;
31  y[ipt] = fplot->Eval(x[ipt]);
32  }
33 
34  TGraphPolar * grP = new TGraphPolar(1000,x,y);
35  grP->SetLineColor(2);
36  grP->SetLineWidth(2);
37  grP->SetFillStyle(3012);
38  grP->SetFillColor(2);
39  grP->Draw("AFL");
40 
41  for (Int_t ipt = 0; ipt < 20; ipt++){
42  xval1[ipt] = x[1000/20*ipt];
43  yval1[ipt] = y[1000/20*ipt];
44  }
45 
46  TGraphPolar * grP1 = new TGraphPolar(20,xval1,yval1);
47  grP1->SetMarkerStyle(29);
48  grP1->SetMarkerSize(2);
49  grP1->SetMarkerColor(4);
50  grP1->SetLineColor(4);
51  grP1->Draw("CP");
52 
53  // Update, otherwise GetPolargram returns 0
54  CPol->Update();
55  grP1->GetPolargram()->SetTextColor(8);
56  grP1->GetPolargram()->SetRangePolar(-TMath::Pi(),TMath::Pi());
57  grP1->GetPolargram()->SetNdivPolar(703);
58  grP1->GetPolargram()->SetToRadian();
59 
60  CPol->cd(2);
61  Double_t x2[30];
62  Double_t y2[30];
63  Double_t ex[30];
64  Double_t ey[30];
65  for (Int_t ipt = 0; ipt < 30; ipt++){
66  x2[ipt] = x[1000/30*ipt];
67  y2[ipt] = 1.2 + 0.4*sin(TMath::Pi()*2*ipt/30);
68  ex[ipt] = 0.2+0.1*cos(2*TMath::Pi()/30*ipt);
69  ey[ipt] = 0.2;
70  }
71 
72  TGraphPolar * grPE = new TGraphPolar(30,x2,y2,ex,ey);
73  grPE->SetMarkerStyle(22);
74  grPE->SetMarkerSize(1.5);
75  grPE->SetMarkerColor(5);
76  grPE->SetLineColor(6);
77  grPE->SetLineWidth(2);
78  grPE->Draw("EP");
79  CPol->Update();
80  grPE->GetPolargram()->SetTextSize(0.03);
81  grPE->GetPolargram()->SetTwoPi();
82  grPE->GetPolargram()->SetToRadian();
83 }