Multipalette with transparency

I would like to superimpose two 2D histograms plotted with option “col” and two different palettes and each plot with certain degree of transparency. I tried to do it with the example in multipalette.C, by adding transparency in the definition of the palettes. It did not work for me. I am attaching the modified example. Any help would be appreciated.

/// \file
/// \ingroup tutorial_graphs
/// \notebook
/// Draw color plots using different color palettes.
///
/// \macro_image
/// \macro_code
///
/// \author Olivier Couet

#include "TStyle.h"
#include "TColor.h"
#include "TF2.h"
#include "TExec.h"
#include "TCanvas.h"

void Pal1()
{
   static Int_t  colors[50];
   static Bool_t initialized = kFALSE;

   Double_t Red[3]    = { 1.00, 0.00, 0.00};
   Double_t Green[3]  = { 0.00, 1.00, 0.00};
   Double_t Blue[3]   = { 1.00, 0.00, 1.00};
   Double_t Length[3] = { 0.00, 0.50, 1.00 };

   if(!initialized){
      Int_t FI = TColor::CreateGradientColorTable(3,Length,Red,Green,Blue,50);
      for (int i=0; i<50; i++) colors[i] = FI+i;
      initialized = kTRUE;
      return;
   }
   gStyle->SetPalette(50,colors,0.0);
}

void Pal2()
{
   static Int_t  colors[50];
   static Bool_t initialized = kFALSE;

   Double_t Red[3]    = { 1.00, 0.50, 0.00};
   Double_t Green[3]  = { 0.50, 0.00, 1.00};
   Double_t Blue[3]   = { 1.00, 0.00, 0.50};
   Double_t Length[3] = { 0.00, 0.50, 1.00 };

   if(!initialized){
      Int_t FI = TColor::CreateGradientColorTable(3,Length,Red,Green,Blue,50);
      for (int i=0; i<50; i++) colors[i] = FI+i;
      initialized = kTRUE;
      return;
   }
   gStyle->SetPalette(50,colors,0.);
}

TCanvas *multiPaletteTransparency() {
   TCanvas *c3  = new TCanvas("c3","C3",0,0,600,400);
   gStyle->SetCanvasPreferGL(kTRUE);
   c3->Divide(2,1);
   TF2 *f3 = new TF2("f3","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3);
   f3->SetLineWidth(1);
   f3->SetLineColor(kBlack);

   c3->cd(1);
   f3->Draw("col");
   TExec *ex1 = new TExec("ex1","Pal1();");
   ex1->Draw();
   f3->Draw("col");
   f3->Draw("col same");

   c3->cd(2);
   f3->Draw("col");
   TExec *ex2 = new TExec("ex2","Pal2();");
   ex2->Draw();
   f3->Draw("col same");



   return c3;
}

The transparency does not work for all graphics backends (X11 does not have it for instance)
See:
https://root.cern/doc/master/classTColor.html#C07

@couet Is it even possible to set different palettes for different histograms? I believe my experience indicates that as soon as gStyle->SetPalette is called the previously plotted histograms will change palettes when the pads are updated.

Yes, as shown in this tutorial,

Sorry I should have read the original post more clearly. Thanks for the link.

I used

gStyle->SetCanvasPreferGL(kTRUE);

to set the canvas to OpenGL. Doesn’t that work?

Pablo

it should but it also depends which root version you are using… if it is too old it won’t work.

I just tested the transparency of a figure plotted with "COL" on ROOT 6.10.06 with a TF1 plotted underneath a TF2 on the Cocoa back end and did not get a transparent image as expected. (Also tried including gStyle->SetCanvasPreferGL(true) with no success, not sure it has any affect on a Mac.)

void Pal1() {
   static Int_t  colors[50];
   static Bool_t initialized = kFALSE;

   Double_t Red[3]    = { 1.00, 0.00, 0.00};
   Double_t Green[3]  = { 0.00, 1.00, 0.00};
   Double_t Blue[3]   = { 1.00, 0.00, 1.00};
   Double_t Length[3] = { 0.00, 0.50, 1.00 };

   if(!initialized){
      Int_t FI = TColor::CreateGradientColorTable(3,Length,Red,Green,Blue,50);
      for (int i=0; i<50; i++) colors[i] = FI+i;
      initialized = kTRUE;
   }
   gStyle->SetPalette(50,colors,0.5);
}

void example() {
   Pal1();

   TF1 *f2 = new TF1("f2","x",1,3);
   TF2 *f3 = new TF2("f3","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3);

   f2->Draw();
   f3->Draw("col same");
}

Also, when I try to zoom in I get a weird behavior:

Yes you are right … it works on predefined palette though. Try:

gStyle->SetPalette(kBird,0,0.5);

It might be it is not implemented for user define palette . I will check.

Agreed works with predefined palette (zoom is still an issue).

I am using 6.10/02. Does the example I sent work for you?

I revised your example to use predefined palettes and it sort of works, although the behavior is not what I expected. I would have thought pad 1 is kBird, pad 2 is 'kCherry and pad 3 is a mix, but pads 2 and 3 are switched? If I click on pad 2 it refreshes and turns back to complete kCherry. (Also, worked in 6.10.02 on Cocoa for me.)
revised_example

void Pal1() {
    gStyle->SetPalette(kBird, 0, 0.5);
}

void Pal2() {
  gStyle->SetPalette(kCherry, 0, 0.5);
}

TCanvas *test() {
   TCanvas *c3  = new TCanvas("c3","C3",0,0,600,400);
   gStyle->SetCanvasPreferGL(kTRUE);
   c3->Divide(3,1);
   TF2 *f3 = new TF2("f3","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3);
   TF2 *f4 = new TF2("f4","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3);
   f3->SetLineWidth(1);
   f3->SetLineColor(kBlack);

   c3->cd(1);
   f3->Draw("col");
   TExec *ex1 = new TExec("ex1","Pal1();");
   ex1->Draw();
   f3->Draw("col same");

   c3->cd(2);
   f4->Draw("col");
   TExec *ex2 = new TExec("ex2","Pal2();");
   ex2->Draw();
   f4->Draw("col same");

   c3->cd(3);
   Pal1();
   f3->Draw("col");
   TExec *ex3 = new TExec("ex3","Pal2();");
   ex3->Draw();
   f4->Draw("col same");

   return c3;
}

Yes it is the mixing of TF1 and TF2 lose the zooming mechanism

1 Like

Using this script I can reproduce your intended behavior, but I have to click on pad 2 to get only the transparent kCherry. Also, when changing zooms (in or out) you get different combinations of the palettes in each pad, clicking on it after the zoom restores it to the expected palette.

revised_example2

void Pal1() {
     gStyle->SetPalette(kBird, 0, 0.5);
}

void Pal2() {
   gStyle->SetPalette(kCherry, 0, 0.5);
}

TCanvas *test() {
   TCanvas *c3  = new TCanvas("c3","C3",0,0,600,400);
   gStyle->SetCanvasPreferGL(kTRUE);
   c3->Divide(3,1);
   TF2 *f3 = new TF2("f3","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3);

   c3->cd(1);
   f3->Draw("col");
   TExec *ex1 = new TExec("ex1","Pal1();");
   ex1->Draw();
   f3->Draw("col same");

   c3->cd(2);
   f3->Draw("col");
   TExec *ex2 = new TExec("ex2","Pal2();");
   ex2->Draw();
   f3->Draw("col same");

   c3->cd(3);
   f3->Draw("col");
   TExec *ex3 = new TExec("ex3","Pal1();");
   ex3->Draw();
   f3->Draw("col same");
   TExec *ex4 = new TExec("ex4","Pal2();");
   ex4->Draw();
   f3->Draw("col same");
}

It does not work for me. I get the same for panels 2 and 3. Why doesn’t
it work for custom palettes, anyway? It looks like something fishy is
going on.

@couet Is investigating, we will have to wait to see what he finds.

Ok here is a simpler and working version of the @ksmith example. Now give me some tome to investigate why the user defined one cannot be transparent.

void transpal2() {
   TCanvas *C  = new TCanvas("C","C3",0,0,600,400);
   gStyle->SetCanvasPreferGL(kTRUE);
   C->Divide(3,1);
   TF2 *f3 = new TF2("f3","0.1+(1-(x-2)*(x-2))*(1-(y-2)*(y-2))",1,3,1,3);

   // Pad 1 is kBird
   C->cd(1);
   gStyle->SetPalette(kBird);
   f3->Draw("col");

   // Pad 2 is kCherry
   TExec *ex2 = new TExec("ex2","gStyle->SetPalette(kCherry);");
   ex2->Draw();
   C->cd(2);
   f3->Draw("col");

   // Pad 3 is 0.5 * kBird + 0.5 kCherry
   TExec *ex3 = new TExec("ex3","gStyle->SetPalette(kBird, 0, 0.5);");
   ex3->Draw();
   C->cd(3);
   f3->Draw("col");
   TExec *ex4 = new TExec("ex4","gStyle->SetPalette(kCherry, 0, 0.5);");
   ex4->Draw();
   f3->Draw("col same");

   // Reset kBird
   TExec *ex5 = new TExec("ex2","gStyle->SetPalette(kBird);");
   ex5->Draw();
}

This example is very strange when you click on all the pads (maybe we leave this until the custom palette transparency is resolved first).
Before clicking:
couet
After clicking:
couet_clicked

This canvas need to executed completely to work. If you click you execute only a single pad and the palette sequencing is lost. You can resize though

It does not work for me. I am running on Ubuntu 16.04. Attached is a
screen-shot of what I get. I click on pad 2 and nothing happens. I zoom
in and out, and I don’t see any color changes on any of the panels.
Other examples of transparency work for me, like transp.C.

Pablo