How to rotate a histogram TH1D or a gpad?


Hi, rooters,

I have built two TH2D histogram in a same Pad( defined as pad1), and then projectX and ProjectY as TH1D drawn in two pads. (gpad2, gpad3).

I want to rotate the projectY to parallel to Y-axis of the TH2D, how can I do?

Try with the HBAR draw option.

1 Like

Yes as @dastudillo said HBAR is the way. Here is an example:

{
   TCanvas *c1 = new TCanvas("c1", "c1",900,900);
   gStyle->SetOptStat(0);
   
   TPad *center_pad = new TPad("center_pad", "center_pad",0.0,0.0,0.6,0.6);
   center_pad->Draw();

   right_pad = new TPad("right_pad", "right_pad",0.55,0.0,1.0,0.6);
   right_pad->Draw();

   top_pad = new TPad("top_pad", "top_pad",0.0,0.55,0.6,1.0);
   top_pad->Draw();

   TH2F *h2 = new TH2F("h2","",40,-4,4,40,-20,20);
   Float_t px, py;
   for (Int_t i = 0; i < 25000; i++) {
      gRandom->Rannor(px,py);
      h2->Fill(px,5*py);
   }
   TH1D * projh2X = h2->ProjectionX();
   TH1D * projh2Y = h2->ProjectionY();

   center_pad->cd();
   gStyle->SetPalette(1);
   h2->Draw("COL");

   top_pad->cd();
   projh2X->SetFillColor(kBlue+1);
   projh2X->Draw("bar");

   right_pad->cd();
   projh2Y->SetFillColor(kBlue-2);
   projh2Y->Draw("hbar");
   
   c1->cd();
   TLatex *t = new TLatex();
   t->SetTextFont(42);
   t->SetTextSize(0.02);
   t->DrawLatex(0.6,0.88,"This example demonstrate how to display");
   t->DrawLatex(0.6,0.85,"a histogram and its two projections.");
}

1 Like

Thank you, couet. It helps me a lot! :smiley:

I have one question, how to draw the projectionY in the left_pad that is mirror symmetrical to the graph on the right_pad?

No, that’s not possible with a simple option.

fine. Thank you very much.
Another question, if TH1D draw option is “hbar”, what option can change TH1D draw style, such as LineWidth? LineColor?

I tried hist->SetLineWidth(2); hist->SetLineColor(kRed);
They didn’t work.

Best regards,
hurricane1127

Bars are always drawn as filled polygons.

I didn’t find the style about “filled polygons”…
such as how to change the fill color of the “polygons”?

Thank you for your reply!

projh2Y->SetFillColor(2);
1 Like

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