THStack "pfc" transparent fill?

There does not seem to be a way to plot a THStack with the “pfc” option and have the fill partially transparent. Is there a way to draw the THStack with this option and also use FIllColorAlpha? The following code produces opaque histograms only:

   auto h1st = new TH1F("h1st", "test hstack", 100, -4, 4);
   h1st->FillRandom("gaus", 20000);
   hs->Add(h1st);

   auto h2st = new TH1F("h2st", "test hstack", 100, -4, 4);
   h2st->FillRandom("gaus", 20000);
   hs->Add(h2st);

   hs->Draw("pfc nostack");

ROOT Version: 6.30/06
Platform: lxplus

Welcome to the ROOT forum @rlyons.

@couet do you have an idea on how to use transparency with THStack and pfc (palette fill color)?
I tried to define a transparent palette, but transparency is ignored.

{
   Int_t colors[2] = {kBlue, kRed}; 
   gStyle->SetPalette(2, colors, 0.5);

   THStack *hs = new THStack("hs","Stacked 1D histograms");
   auto h1st = new TH1F("h1st", "test hstack", 100, -4, 4);
   h1st->FillRandom("gaus", 100000);
   hs->Add(h1st);

   auto h2st = new TH1F("h2st", "test hstack", 100, -4, 4);
   h2st->FillRandom("gaus", 20000);
   hs->Add(h2st);

   hs->Draw("pfc nostack");
}

I’m not entirely sure but I think one thing that’s probably needed for transparency is gStyle->SetCanvasPreferGL(true);, but even with that, this example fails. However, using the pre-defined colour palettes (instead of the user-defined one) seems to work as expected:

 {
   gStyle->SetCanvasPreferGL(true);   // no transparency without this line?

   Int_t colors[2] = {kBlue, kRed}; 
   //gStyle->SetPalette(2, colors, 0.30);
   gStyle->SetPalette(59, 0, 0.30);

   THStack *hs = new THStack("hs","Stacked 1D histograms");
   auto h1st = new TH1F("h1st", "test hstack", 100, -4, 4);
   h1st->FillRandom("gaus", 100000);
   hs->Add(h1st);

   auto h2st = new TH1F("h2st", "test hstack", 100, -4, 4);
   h2st->FillRandom("gaus", 20000);
   hs->Add(h2st);

   hs->Draw("pfc nostack");

}

Hi,

Transparent colors correctly working only on Mac.

And in web canvas.

Regards,
Sergey

Yes, using the pre-defined palette works to make the fill transparent by simply adding gStyle->SetPalette(59, 0, 0.30);

However this also makes the lines transparent and for some reason they appear inside of the fill when using “plc pfc” option. Any ideas on how to make the fill transparent yet have a thick opaque line of the same colour?

{
   gStyle->SetPalette(59, 0, 0.30);

   THStack *hs = new THStack("hs","Stacked 1D histograms");
   auto h1st = new TH1F("h1st", "test hstack 1", 100, -4, 4);
   h1st->FillRandom("gaus", 10000);
   h1st->SetLineWidth(3);
   hs->Add(h1st);

   auto h2st = new TH1F("h2st", "test hstack 2", 100, -4, 4);
   h2st->FillRandom("gaus", 2000);
   hs->Add(h2st);

   TCanvas *c = new TCanvas("plotg","",1500,1000);
   hs->Draw("pfc plc nostack");
   gPad->BuildLegend(0.75, 0.75, 0.95, 0.95, "", "L");
}

This macro works for me:

{
   gStyle->SetCanvasPreferGL(true);

   THStack *hs = new THStack("hs","Stacked 1D histograms");
   auto h1st = new TH1F("h1st", "test hstack 1", 100, -4, 4);
   h1st->FillRandom("gaus", 10000);
   h1st->SetLineWidth(3);
   h1st->SetFillColor(kRed);
   h1st->SetFillStyle(3144);
   hs->Add(h1st);

   auto h2st = new TH1F("h2st", "test hstack 2", 100, -4, 4);
   h2st->FillRandom("gaus", 2000);
   h2st->SetFillColor(TColor::GetColor((Float_t) 0., 1., 0., 0.3));
   hs->Add(h2st);

   TCanvas *c = new TCanvas("plotg","",1500,1000);
   hs->Draw("nostack");
   gPad->BuildLegend(0.75, 0.75, 0.95, 0.95, "", "L");
}

Here only color of second histogram made transparent.
Also “pfc” and “plc” options are removed.

This works but the point was not to have to set the line colour and fill of each histogram separately, as is possible using a pre-defined palette with “plc” and “pfc”. I would also like to use a fully transparent fill, not hatches. I take it that there is no way to use a THStack with these options to produce a solid line and transparent fill of the same colour.

Hi,

If you use “plc” and “pfc” at the same time - THStack will use same color palette for line colors and fill colors. Therefore the only solution for you - skip “plc” option and set line colors yourself.

I put hatches to clearly demonstrate when transparent working or not. You can skip it if you want.

Since there is no way to do this with the pre-defined palette I have achieved the desired result by setting the fill and line colours by hand:

{
   THStack *hs = new THStack("hs","Stacked 1D histograms");
   auto h1st = new TH1F("h1st", "test hstack 1", 100, -4, 4);
   h1st->FillRandom("gaus", 10000);
   h1st->SetLineWidth(2);
   h1st->SetLineColor(2);
   h1st->SetFillColorAlpha(2, 0.3);
   hs->Add(h1st);

   auto h2st = new TH1F("h2st", "test hstack 2", 100, -4, 4);
   h2st->FillRandom("gaus", 2000);
   h2st->SetLineWidth(2);
   h2st->SetLineColor(4);
   h2st->SetFillColorAlpha(4, 0.3);
   hs->Add(h2st);

   TCanvas *c = new TCanvas("","",1500,1000);
   hs->Draw("nostack");
   gPad->BuildLegend(0.75, 0.75, 0.95, 0.95, "", "L");
}

Many thanks!

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