# Overlaying histograms with TAttFill

**URL:** <https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437>\
**Category:** Newbie\
**Created:** [November 25, 2025, 10:09am UTC](https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437 "2025-11-25T10:09:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [November 25, 2025, 10:09am UTC](https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437/1 "2025-11-25T10:09:23Z")

</div>

_ROOT Version:_ 6.32.04  
_Platform:_ WSL2 Ubuntu

Hello! I want to plot 7 histograms on top of each other, in black and white, each with their own fill pattern. Since I don’t want the fill pattern of the underneath histos to be seen, I tried setting the FillColorAlpha of each histo to an opaque white. Below is a code snippet:

```apache
hist_01->SetFillStyle(3001); 
hist_01->SetFillColorAlpha(kWhite,1); 
hist_01->SetLineColor(kBlack);

hist_02->SetFillStyle(3344); 
hist_02->SetFillColorAlpha(kWhite,1); 
hist_02->SetFillColor(kBlack); 
hist_02->SetLineColor(kBlack);
.
.
.
.
hist_07->SetFillStyle(3409); 
hist_07->SetFillColorAlpha(kWhite,1); 
hist_07->SetFillColor(kBlack); 
hist_07->SetLineColor(kBlack);

```

When I do this, I get the following output which does not retain the opaqueness of each layer.

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/e/7/e770d0a2ea1824ee7a4dbdcf3552b9f267010469.png)

Additionally, I did try the THStack function as below:

```apache
auto hist_stk= new THStack("","");
hist_stk->Add(hist_01);
hist_stk->Add(hist_02);
hist_stk->Add(hist_03);
hist_stk->Add(hist_04);
hist_stk->Add(hist_05);
hist_stk->Add(hist_06);
hist_stk->Add(hist_07);
hist_stk->Draw();
lg3->Draw("SAME");

```

But I get the following error:

```auto
Info in <TH1F::Add>: Histograms have different number of bins - trying to use TH1::Merge
Error in <TH1Merger::DifferentAxesMerge>: Cannot merge histograms - the histograms hist_04 can extend the X axis or have different limits and underflows/overflows are present in the histogram hist_04.

```

Hope to get some help! Thanks in advance.

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [November 25, 2025, 10:17am UTC](https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437/2 "2025-11-25T10:17:10Z")

</div>

As the [documentation](https://root.cern.ch/doc/v632/classTAttFill.html) is telling:

> **Color transparency**
> 
> The transparency is available on all platforms when the flag `OpenGL.CanvasPreferGL` is set to `1` in `$ROOTSYS/etc/system.rootrc`, or on Mac with the Cocoa backend. On the file output it is visible with PDF, PNG, Gif, JPEG, SVG, TeX… but not PostScript.
> 
> Alternatively, you can call at the top of your script `gSytle->SetCanvasPreferGL();`. Or if you prefer to activate GL for a single canvas `c`, then use `c->SetSupportGL(true);`.

---

<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:** [November 25, 2025, 10:57am UTC](https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437/3 "2025-11-25T10:57:17Z")

</div>

A workaround is to draw the histos first with solid white fill and then a clone with the hatched fill:

```auto
{
   auto h1st = new TH1F("h1st", "test hstack", 100, -4, 4);
   h1st->FillRandom("gaus", 20000);
   h1st->SetLineColor(kBlack);
 
   auto h2st = new TH1F("h2st", "test hstack", 100, -4, 4);
   h2st->FillRandom("gaus", 15000);
   h2st->SetLineColor(kBlack);

   auto h3st = new TH1F("h3st", "test hstack", 100, -4, 4);
   h3st->FillRandom("gaus", 10000);
   h3st->SetLineColor(kBlack);

   h1st->SetFillColor(10);
   h1st->SetFillStyle(1001);
   h1st->Draw();
   auto h1b = (TH1F*)h1st->Clone("h1b");
   h1b->SetFillColor(kBlack);
   h1b->SetFillStyle(3005);
   h1b->Draw("same");

   h2st->SetFillStyle(1001);
   h2st->SetFillColor(10);
   h2st->Draw("same");
   auto h2b = (TH1F*)h2st->Clone("h2b");
   h2b->SetFillColor(kBlack);
   h2b->SetFillStyle(3344);
   h2b->Draw("same");

   h3st->SetFillStyle(1001);
   h3st->SetFillColor(10);
   h3st->Draw("same");
   auto h3b = (TH1F*)h3st->Clone("h3b");
   h3b->SetFillColor(kBlack);
   h3b->SetFillStyle(3409);
   h3b->Draw("same");
}

```

 ![myc](https://root-forum.cern.ch/uploads/default/original/3X/5/1/51dc234826dffc3bca64a76e32e80ecb45089df2.png)

---

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [November 25, 2025, 11:41am UTC](https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437/4 "2025-11-25T11:41:02Z")

</div>

It’s a longer code but works perfectly! One question, how do I get the legend to show the right patterns? I get this now.

![image](https://root-forum.cern.ch/uploads/default/original/3X/3/e/3e0c53f9796975007fb2fd515736750e8c935b28.png)

---

<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:** [November 25, 2025, 11:44am UTC](https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437/5 "2025-11-25T11:44:03Z")

</div>

Create your own TLegend ( [ROOT: TLegend Class Reference](https://root.cern/doc/master/classTLegend.html) ) and only add (with the option “F”) the pattern-filled histos.

---

<div class="post-metadata">

**Author:** ![phylmath](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/phylmath/32/38400_2.png) [@phylmath](https://root-forum.cern.ch/u/phylmath)\
**Post date:** [November 25, 2025, 11:46am UTC](https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437/6 "2025-11-25T11:46:57Z")

</div>

Thanks! I also noticed the x-axis ticks being covered by the histos. How could I fix this?

![image](https://root-forum.cern.ch/uploads/default/original/3X/6/b/6bcdc5e8dd4a3511914508ddf60b056066f3702e.png)

---

<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:** [November 25, 2025, 11:52am UTC](https://root-forum.cern.ch/t/overlaying-histograms-with-tattfill/64437/7 "2025-11-25T11:52:55Z")

</div>

> [@How to redraw Axis and plot borders?](https://root-forum.cern.ch/t/how-to-redraw-axis-and-plot-borders/28252):
>
> Sometimes, when several plots are drawn in the same pad using the option SAME, the axis tick marks and the plot’s border lines might be erased by the front plots. It might be useful to redraw them. The following example shows how to proceed. void redrawBorder() { gPad-\>Update(); gPad-\>RedrawAxis(); TLine l; l.DrawLine(gPad-\>GetUxmin(), gPad-\>GetUymax(), gPad-\>GetUxmax(), gPad-\>GetUymax()); l.DrawLine(gPad-\>GetUxmax(), gPad-\>GetUymin(), gPad-\>GetUxmax(), gPad-\>GetUymax()); } void…
