# Applying label for Categorical variable

**URL:** <https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488>\
**Category:** Newbie\
**Created:** [August 27, 2021, 9:10am UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488 "2021-08-27T09:10:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilomidze](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@ilomidze](https://root-forum.cern.ch/u/ilomidze)\
**Post date:** [August 27, 2021, 9:11am UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/1 "2021-08-27T09:11:00Z")

</div>

\_ROOT Version: 6.22/08  
\_Platform: macosx64  
_Compiler:_ Not Provided

I have experiment names with corresponding experimental numerical result.  
I want to have experiment names on X axis and their values on Y.

I can plot it using TGraph, but I dont know how to change x value labels to experiment names.

```auto
    int x[arrSize];
    int y[arrSize];
    for (int i = 0; i < arrSize; i++) {
        x[i] = i; 
        y[i] = dataBase[i]; // some value from the experiment
    }

    auto gr = new TGraph(arrSize, x, y);
    gr->SetLineWidth(3);
    gr->SetMarkerStyle(21);
    gr->SetMarkerSize(0.5);
    gr->SetLineColor(2);
    gr->Draw();

```

So, this way I can see the result and how it is changing, but I want to add some names/labels on X axis: e.g. experiment 41, experiment 55 etc.

Experiments which I am plotting are filtered, so numbers are not consecutive and I really need to show which experiments I am talking

---

<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:** [August 27, 2021, 9:24am UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/2 "2021-08-27T09:24:32Z")

</div>

May be [this example](https://root.cern/doc/master/labels1_8C.html) can help you ?

---

<div class="post-metadata">

**Author:** ![ilomidze](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@ilomidze](https://root-forum.cern.ch/u/ilomidze)\
**Post date:** [August 27, 2021, 10:04am UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/3 "2021-08-27T10:04:20Z")

</div>

It works, but there is 1 problem:  
Labels are not below the point - they are “stick together” and doesn’t even cover the whole x axis.

Here is the changed code

```auto
    int x[arrSize];
    int y[arrSize];
    for (int i = 0; i < arrSize; i++) {
        x[i] = i; 
        y[i] = dataBase[i]; // some value from the experiment
    }

    auto gr = new TGraph(arrSize, x, y);

    for (int i = 1; i <= arrSize; i++) {
        gr->GetXaxis()->SetBinLabel(i, experimentNamesInDatabase[i-1].c_str());
    }

    gr->SetLineWidth(3);
    gr->SetMarkerStyle(21);
    gr->SetMarkerSize(0.5);
    gr->SetLineColor(2);
    gr->Draw();

```

---

<div class="post-metadata">

**Author:** ![ilomidze](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@ilomidze](https://root-forum.cern.ch/u/ilomidze)\
**Post date:** [August 27, 2021, 10:05am UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/4 "2021-08-27T10:05:26Z")

</div>

![Screenshot 2021-08-27 at 12.05.02](https://root-forum.cern.ch/uploads/default/original/3X/5/9/59e710187bc6ef31b00ace380f488f20a0796317.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:** [August 27, 2021, 10:27am UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/5 "2021-08-27T10:27:23Z")

</div>

You need to call [LabelsDeflate](https://root.cern/doc/master/classTH1.html#af6d06f746697e466901773d7ee2307ce)

---

<div class="post-metadata">

**Author:** ![ilomidze](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@ilomidze](https://root-forum.cern.ch/u/ilomidze)\
**Post date:** [August 27, 2021, 10:40am UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/6 "2021-08-27T10:40:31Z")

</div>

I guess TGraph does not have function LabelsDeflate()

At least I can’t use it like:

```auto
gr->LabelsDeflate()

```

---

<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:** [August 27, 2021, 12:13pm UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/7 "2021-08-27T12:13:17Z")

</div>

```auto
gr->GetHistogram()->LabelsDeflate()

```

---

<div class="post-metadata">

**Author:** ![ilomidze](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/e79b87/32.png) [@ilomidze](https://root-forum.cern.ch/u/ilomidze)\
**Post date:** [August 27, 2021, 1:52pm UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/8 "2021-08-27T13:52:57Z")

</div>

I noticed another problem:  
when I apply “gr-\>GetHistogram()-\>LabelsDeflate()” - _Graph gets cropped_.

I guess labels doesn’t spread to the axis, but I am only able to see the part of the graph where the labels are being applied.

 ![Screenshot 2021-08-27 at 18.22.15](https://root-forum.cern.ch/uploads/default/original/3X/3/f/3f3590310b2ea94ee249d307e7358727da672627.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:** [August 30, 2021, 2:10pm UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/9 "2021-08-30T14:10:13Z")

</div>

You should enlarge the Pad bottom margin.

---

<div class="post-metadata">

**Author:** ![Labadie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/50afbb/32.png) [@Labadie](https://root-forum.cern.ch/u/Labadie)\
**Post date:** [September 14, 2021, 6:07am UTC](https://root-forum.cern.ch/t/applying-label-for-categorical-variable/46488/10 "2021-09-14T06:07:18Z")

</div>

Thanks for update and quick reply. I’ll be sure to keep an eye on this thread. Looking for the same info.
