Set User defined Palette of 2D histogram

Hello,
I am trying to plot a 2D histogram using root v. 5.34/36. I want to set the palette so that the color of the bin below a certain value of Z is white color. Currently I am using:

Int_t n =t->GetEntries();
for(Int_t i=0;i<n;i++){
    t->GetEntry(i);   
        h5->Fill(x,y);
}
 const Int_t Number = 3;
Double_t Red[Number]    = { 0,1,1};
Double_t Green[Number]  = { 0,1,0};
Double_t Blue[Number]   = { 1,0,1};
Double_t Length[Number] = { 0.0, 0.30, 1.00 };
Int_t nb=30;
   TColor::CreateGradientColorTable(Number,Length,Red,Green,Blue,nb);
    h5->SetContour(nb);


h5->Draw("LEGO2");

I would appreciate any input. Thanks


_ROOT Version:5.34/36
Platform: Not Provided
Compiler: Not Provided


Hi
here an example:

#include "TCanvas.h"
#include "TH2.h"
#include "TRandom.h"
void testgl()
{
//	gStyle->SetCanvasPreferGL(kTRUE);
	TCanvas *cc= new TCanvas();
	TH2F* h2 = new TH2F("h2", "h2", 50,-10,10, 50,-10,10);
	Double_t x,y;
	Int_t n = 10000;
	while (n-- > 0) {
		gRandom->Rannor(x,y);
		h2->Fill(x,y);
	}
	const Int_t nlevels = 6;
	Double_t clevels[nlevels] = {0, 100, 150, 180, 210, 240};
	Int_t colors[nlevels] = {kWhite, kBlack, kRed, kGreen, kBlue, kMagenta};
	h2->SetContour(nlevels, clevels);
        gStyle->SetPalette(nlevels, colors);
	h2->Draw("LEGO2");
//	h2->Draw("LEGO2GL");
}

Cheers
Otto

Hi Otto,

Thanks for the example, Is there an option to create gradient colors such as:
TColor::CreateGradientColorTable?

try this and play with colors and bias etc.

#include "TStyle.h"
#include "TCanvas.h"
#include "TH2.h"
#include "TRandom.h"

void testgl()
{
// gStyle->SetCanvasPreferGL(kTRUE);
   TCanvas *cc= new TCanvas();
   TH2F* h2 = new TH2F("h2", "h2", 50,-10,10, 50,-10,10);
   Double_t x,y;
   Int_t n = 100000;
   while (n-- > 0) {
      gRandom->Rannor(x,y);
      h2->Fill(x,y);
   }
   // fix color below bias
   Int_t bias = 0.5 * h2->GetMaximum();
   
   const Int_t Number = 3;
   Double_t Red[Number]    = { 1.00, 0.00, 0.00};
   Double_t Green[Number]  = { 0.00, 1.00, 0.00};
   Double_t Blue[Number]   = { 0.00, 0.00, 1.00};
   Double_t Length[Number] = { 0.00, 0.50, 1.00 };
   const Int_t nlevels = 50;
   Int_t MyPalette[nlevels];
   Int_t FI = TColor::CreateGradientColorTable(Number,Length,Red,Green,Blue,nlevels);
   for (int i=0;i<nlevels;i++) MyPalette[i] = FI+i;
   
   Double_t clevels [nlevels];
   // between 0 and bias color should be kWhite
   clevels[0] = 0;
   clevels[1] = bias;
//   MyPalette[0] = kBlack;
   MyPalette[0] = kWhite;
   // the rest continous
   Double_t dz =  (h2->GetMaximum() - bias) / (Double_t)(nlevels-2);
   for (Int_t k=2; k < nlevels; k++) {
      clevels[k] = bias + (k-1)* dz;
   }
   h2->SetContour(nlevels, clevels);
   gStyle->SetPalette(nlevels, MyPalette);
// h2->Draw("SURF1");
   h2->Draw("LEGO2 0"); // dont draw empty bins
// h2->Draw("LEGO2GL");
}

Cheers
Otto

1 Like

that worked… thanks!

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