Isolines colors on a TH2

Hi!

Run this:

vector<double> contour = {50, 300, 800};

void test ()
{
	TCanvas *c1 = new TCanvas("c1", "c1",900,900);
	gStyle->SetOptStat(0);

	// Create, fill and project a 2D histogram.
	TH2F *h2 = new TH2F("h2","",40,-4,4,40,-20,20);
	Float_t px, py;
	for (Int_t i = 0; i < 250000; i++) {
		gRandom->Rannor(px,py);
		h2->Fill(px, 5*py);
	}


	gStyle->SetPalette(1);
	h2->DrawCopy("COLZ");
	
	if (contour.size() > 0) {
		h2->SetContour(contour.size(), &contour[0]);
		h2->SetLineWidth(1);
		h2->SetLineColor(2);
		h2->Draw("cont3 same");
	}

}

You will get a colored map showing 3 superimposed isolines.

Those isolines are red, because I set them to red by using

h2->SetLineColor(2);

How can I give a different color to each contour line/isoline?

Hi
use the LIST option to produce a list of TGraphs representing
the contours.
Details here:
https://root.cern.ch/doc/master/classTHistPainter.html#HP16a

Otto

1 Like
vector<double> contour = {50, 300, 800};

void ziel1(){
   TCanvas *c1 = new TCanvas("c1", "c1",600,400);
   gStyle->SetOptStat(0);

   // Create, fill and project a 2D histogram.
   TH2F *h2 = new TH2F("h2","",40,-4,4,40,-20,20);
   Float_t px, py;
   for (Int_t i = 0; i < 250000; i++) {
      gRandom->Rannor(px,py);
      h2->Fill(px, 5*py);
   }

   if (contour.size() > 0) {
      h2->SetContour(contour.size(), &contour[0]);
      h2->Draw("cont list");
      gPad->Update();
   }

   TObjArray *contours = (TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours");
   Int_t ncontours     = contours->GetSize();
   TList *l0         = (TList*)contours->At(0);
   TList *l1         = (TList*)contours->At(1);
   TList *l2         = (TList*)contours->At(2);

   TGraph *gr0 = (TGraph*)l0->First();
   TGraph *gr1 = (TGraph*)l1->First();
   TGraph *gr2 = (TGraph*)l2->First();

   auto *h2c = (TH2F*)h2->DrawCopy("COLZ");
   h2c->SetContour(100);
   gr0->SetLineWidth(3); gr0->SetLineColor(kRed); gr0->Draw("L");
   gr1->SetLineWidth(3); gr1->SetLineColor(kOrange);gr1->Draw("L");
   gr2->SetLineWidth(3); gr2->SetLineColor(kCyan);gr2->Draw("L");
}

1 Like

I’m a bit surprised that there’s not a single command for that.

Thank you to both of you!

You can plot contours with color but the color are picked in a palette.
Here you want to access each individual contours. That’s why it is more complex

Oh!
Actually I don’t care too much about selecting the color. I only want use different colors, as long as they clearly emerge from the colored map underneath.
Let’s say that I’d like to choose which palette is used for the isolines, from the standard palettes (kBird, KRainBow, kOcean, etc). Is there a shorter way for doing that?

CONT1 is the option
https://root.cern.ch/doc/master/classTHistPainter.html#HP16

1 Like

Ok.
I feel like “I could’ve find it by myself, without having to bother anybody”.

Thank you…

May be :slight_smile:

1 Like

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