# Change the z-axis scale of log-scaled TH2s

**URL:** <https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637>\
**Category:** Evolution Proposals, Apps & Ideas\
**Created:** [February 7, 2019, 2:44pm UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637 "2019-02-07T14:44:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [February 7, 2019, 2:44pm UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637/1 "2019-02-07T14:44:45Z")

</div>

Hi,

when I set the `SetLogz()` for a canvas with a `TH2`, I noticed the minimum of the z-axis of the histogram can go much lower than needed. This is pretty bad as it shrinks (from below) the possible color range for the bins. So I wrote a small function that takes a histogram, scans over all bins and finds the one with min bin content (zero content does not count). I’m aware about the `GetMinimum()` function, but it accounts for non-filled bins and returns 0 in most of my cases. Here is the function:

```auto
Double_t getNextToMinimumBinContent(TH2* histo)
{
        Double_t tempMin = 999.;
        if (histo)
        {
                for (Int_t binx = 0; binx<=histo->GetNbinsX(); binx++)
                {
                        for (Int_t biny = 0; biny<=histo->GetNbinsY(); biny++)
                        {
                                if (histo->GetBinContent(binx, biny) <= tempMin && histo->GetBinContent(binx, biny) > 1e-50)
                                        tempMin = histo->GetBinContent(binx, biny);
                        }
                }
        }
        return tempMin;
}

```

Its usage is trivial:

```auto
some_2D_histogram->SetMinimum(getNextToMinimumBinContent(some_2D_histogram));

```

I think it’d be great if this was the default behaviour.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 7, 2019, 4:24pm UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637/2 "2019-02-07T16:24:15Z")

</div>

> [@yus](#):
>
> when I set the `SetLogz()` for a canvas with a `TH2` , I noticed the minimum of the z-axis of the histogram can go much lower than needed.

Do you have some reproducer showing this effect ?

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [February 7, 2019, 8:41pm UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637/3 "2019-02-07T20:41:56Z")

</div>

Sure Olivier, here is the reproducer:

```auto
void zAxisInLogScale_Reproducer()
{
        TCanvas* canv = new TCanvas("canv", "My canvas", 0, 0, 800, 600);
        canv->SetRightMargin(0.18);
        TH2F* hist1 = new TH2F("hist1", "hist 1;X;Y;Z", 50, 0., 50., 50, 0., 50.);
        for (Int_t i=1; i<=hist1->GetNbinsX(); i++)
                hist1->Fill(i, i, i);
        hist1->Draw("colz");
        canv->SetLogz();
        return;
}

```

Clearly, all the bins are filled with values \>=1. But the z-axis is well below this minimum value (it goes down to 0.05), restricting the colour palette to its upper ~half:

 ![reproducer](https://root-forum.cern.ch/uploads/default/original/3X/b/f/bfe840f84ace92a69746f2d190213e9b70c11f97.png)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 8, 2019, 8:17am UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637/4 "2019-02-08T08:17:51Z")

</div>

> [@yus](#):
>
> Clearly, all the bins are filled with values \>=1.

Not at all … Only the diagonale is filled all the rest is 0. Therefore for the Z scale in log scale a minimum value \>0 has to be set. Do 'hist1-\>SetMinimum(1);`an the whole palette will be used.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [February 8, 2019, 8:39am UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637/5 "2019-02-08T08:39:05Z")

</div>

```auto
hist1->SetMinimum(hist1->GetMinimum(0.)); // black magic
```

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [February 8, 2019, 10:43am UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637/6 "2019-02-08T10:43:43Z")

</div>

> [@couet](#):
>
> Do `hist1->SetMinimum(1);`

Yes, and I use my function to figure out what new minimum I should use. It’s `1` for this case clearly, but sometimes it’s not that easy to figure out. Anyway, Wile\_E\_Coyote suggested a better solution below.

---

<div class="post-metadata">

**Author:** ![yus](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/y/9fc348/32.png) [@yus](https://root-forum.cern.ch/u/yus)\
**Post date:** [February 8, 2019, 10:46am UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637/7 "2019-02-08T10:46:45Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> `hist1->SetMinimum(hist1->GetMinimum(0.)); // black magic`

This does exactly what I wanted. I wasn’t aware I could feed the `GetMinimum()` some parameter. I guess I kind of reimplemented that in my function 🤦‍♂️

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [February 8, 2019, 11:35am UTC](https://root-forum.cern.ch/t/change-the-z-axis-scale-of-log-scaled-th2s/32637/8 "2019-02-08T11:35:36Z")

</div>

> [@yus](#):
>
> Anyway, Wile\_E\_Coyote suggested a better solution below.

Yes it is more general and I should have proposed you this solution right away. But anyway it is the same idea. Your histogram is full of 0 and that does not work in log scale.
