# THStack "pfc" transparent fill?

**URL:** <https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439>\
**Category:** ROOT\
**Tags:** hist\
**Created:** [April 23, 2025, 2:11pm UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439 "2025-04-23T14:11:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rlyons](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@rlyons](https://root-forum.cern.ch/u/rlyons)\
**Post date:** [April 23, 2025, 2:11pm UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/1 "2025-04-23T14:11:32Z")

</div>

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
   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

---

<div class="post-metadata">

**Author:** ![lbreitwi](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/lbreitwi/32/21330_2.png) [@lbreitwi](https://root-forum.cern.ch/u/lbreitwi)\
**Post date:** [April 23, 2025, 3:10pm UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/2 "2025-04-23T15:10:57Z")

</div>

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.

```cpp
{
   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");
}

```

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [April 24, 2025, 6:27am UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/3 "2025-04-24T06:27:49Z")

</div>

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](https://root.cern/doc/master/classTColor.html#C06) (instead of the user-defined one) seems to work as expected:

```auto
 {
   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");

}

```

 ![c1](https://root-forum.cern.ch/uploads/default/original/3X/9/d/9d3ff74286efbb1f59b54dafed8dda6a31773735.png)

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [April 24, 2025, 7:22am UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/4 "2025-04-24T07:22:27Z")

</div>

Hi,

Transparent colors correctly working only on Mac.

And in web canvas.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![rlyons](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@rlyons](https://root-forum.cern.ch/u/rlyons)\
**Post date:** [April 24, 2025, 7:25am UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/5 "2025-04-24T07:25:14Z")

</div>

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?

```auto
{
   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");
}

```

 ![Screenshot from 2025-04-24 09-24-46](https://root-forum.cern.ch/uploads/default/original/3X/d/1/d1053aa7d98e4ee15ea106cfbd5198b079e1bb9a.png)

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [April 24, 2025, 7:56am UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/6 "2025-04-24T07:56:51Z")

</div>

This macro works for me:

```auto
{
   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.

 ![plotg](https://root-forum.cern.ch/uploads/default/original/3X/a/4/a4b7c396192ce1dfa5bf4f33e4cbdacaa8027a12.png)

---

<div class="post-metadata">

**Author:** ![rlyons](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@rlyons](https://root-forum.cern.ch/u/rlyons)\
**Post date:** [April 24, 2025, 9:21am UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/7 "2025-04-24T09:21:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [April 24, 2025, 11:12am UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/8 "2025-04-24T11:12:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rlyons](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@rlyons](https://root-forum.cern.ch/u/rlyons)\
**Post date:** [April 24, 2025, 12:20pm UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/9 "2025-04-24T12:20:39Z")

</div>

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:

```auto
{
   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");
}

```

 ![Screenshot from 2025-04-24 14-19-28](https://root-forum.cern.ch/uploads/default/original/3X/7/a/7a7631fada5bc8832eaa6c9b9defe8c2bc8aff86.png)

Many thanks!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 8, 2025, 12:21pm UTC](https://root-forum.cern.ch/t/thstack-pfc-transparent-fill/63439/10 "2025-05-08T12:21:18Z")

</div>

Closed on May 8, 2025, 12:21pm UTC

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