# 2D Histogram Palette setting

Hi, I’m using ROOT in a lab experiment in university in which we have to make 2D histograms with a colour palette showing the variation in bins in the z-axis.
Up until this point it’s been fine but now we have some negative values, which we need to keep, which results in the bins with ‘0’ getting coloured instead of being white.
I was just wondering if there is a way to specify a value, 0, to be a colour, white, in a 2D ‘colz’ plot or to have one palette for positive values and another for negative, leaving zero white.
My coding ability is basic at best so be gentle please.

Thanks for the quick response.
I’m sure I’ve tried using them before and got nothing, might just be me though. Would you mind using it in some example code please, like I said I barely know coding.
Thanks very much

``````{
TCanvas *c1 = new TCanvas("c1","c1",600,400);
TH2F *hcol2 = new TH2F("hcol2","Option COLor example ",40,-4,4,40,-20,20);
Float_t px, py;
for (Int_t i = 0; i < 1000000; i++) {
gRandom->Rannor(px,py);
px=TMath::Abs(px);
py=TMath::Abs(py);
hcol2->Fill( 5*px, 5*py,1.);
hcol2->Fill(-5*px,-5*py,-1.);
}
const UInt_t Number = 5;
Double_t Red[Number]    = { 1.00, 0.00, 1.00, 1.00, 0.00};
Double_t Green[Number]  = { 0.00, 1.00, 1.00, 0.00, 0.00};
Double_t Blue[Number]   = { 0.00, 0.00, 1.00, 1.00, 1.00};
Double_t Length[Number] = { 0.00, 0.25, 0.50, 0.75, 1.00 };
Int_t nb=50;

hcol2->SetContour(nb);
hcol2->SetMaximum(6000);
hcol2->SetMinimum(-6000);
hcol2->Draw("colz");
}``````

Thanks for the responses guys.
I couldn’t get either to do what I wanted but maybe it’s just me being dumb.

This is the code we have so far for one of the 2D histograms being plotted

TCanvas *c1 = new TCanvas(“c1”,"",600,400);
c1->cd();
KDpN->SetStats(0);
KDpN->GetXaxis()->SetTitle(“Kaon-Pion Mass Squared (GeV^2/c^4)”);
KDpN->GetYaxis()->SetTitle(“Pion-Pion Mass Squared (GeV^2/C^4”);
KDpN->SetContour(99);
KDpN->Draw(“colz TEXTnn”);
c1->SaveAs(“LocalCP1.pdf”);

and the histogram it plots is attached.

The first reponse confused me (my fault not yours) but the second one assumed the 0 point was in the centre of the colour scale. Is the any way for the colour scale to “know” which value is 0 and rescale accordingly, as some of the 0’s are in the centre but some, like in this, are at the bottom but with important negatives below it, or am I asking too much.
LocalCP1.pdf (16.3 KB)

In my example I set the 0 in the middle of the Z-Axis in order to make the example simpler.

The position of the color in the palette is set via the Length parameter.
This parameter gives the position of the color in the palette in a normalised way.

In the example white (r,g,b = 1,1,1) is at the position 0.5 ==> in the middle.

Now you can compute yourself the right position of the value 0 and set the Length value properly.

if z if the position of zero between zmin and zmax.
zn the position of zero between 0 and 1

then you have

(zmax-z)/(zmax-zmin) = (1-zn)/(1-0)
=> zmax/(zmax-zmin) = (1-zn)
=> zn = 1-(zmax/(zmax-zmin))

Actually, I think I got this now. Will let you know

Actually, one last thing.
Because the value for zero moves around I believe I have to create a new colour palette every histogram with the zero value set to white (let me know if I’m wrong). I got the first one to work but the second histogram is using the firsts palette, how do I make the second histogram use the second palette etc (there are 10 in total)?

Yes in principle… but that’s not a good idea if you want to compare your histograms. The best would be to do a first preprocessing finding a common maximum and minimum to all your histograms and set the maximum and minimum to these common values for all of them. After that you will need only one palette and you will be able to compare them easily.

This is the problem, for most of them they range from ~150 -> ~-1 but the ones that arent working at the moment range from ~1->~-3 not really leaving much for common limits.
Is there no way to specify a palette to use or something like that?

In that case you should create several palettes. See:
root.cern.ch/drupal/content/how- … ame-canvas

Can you use that TExec function with the CreateGradientColorTable instead of gStyle->SetPalette()?

``````void twopal()
{
TCanvas *c1 = new TCanvas("c1");
TH2F *h1 = new TH2F("h1","h1",40,-4,4,40,-4,4);
TH2F *h2 = new TH2F("h2","h2",40,-4,4,40,-4,4);
Double_t a,b;
for (Int_t i=0;i<5000;i++) {
gRandom->Rannor(a,b);
h1->Fill(a-1.5,b-1.5);
h2->Fill(a+1.5,b+1.5);
}
TExec *ex1 = new TExec("ex1","grey();");
TExec *ex2 = new TExec("ex2","gStyle->SetPalette(53,0,0.4);");
h1->Draw("col");
ex1->Draw();
h1->Draw("col same");
ex2->Draw();
h2->Draw("col same");
}

void grey()
{
Double_t Red   = { 0.10, 0.10};
Double_t Green = { 0.20, 0.50};
Double_t Blue  = { 0.60, 0.90};
Double_t Stops = { 0.00, 1.00};

Int_t nb=50;
Thanks so much for all the help 