Applying label for Categorical variable

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

    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

May be this example can help you ?

1 Like

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

    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();

You need to call LabelsDeflate

I guess TGraph does not have function LabelsDeflate()

At least I can’t use it like:

gr->LabelsDeflate()
gr->GetHistogram()->LabelsDeflate()
1 Like

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.

You should enlarge the Pad bottom margin.

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