Ugly tick labels

Good afternoon!

I want to do some drawing, but obtain ugly tick labels:

  • the ‘-’ sign coincides with the digits for negative numbers;
  • alignment;
  • size.

Below there is a sample code along with the result.

#include <TAxis.h>

void uglyTLabels(){
    TCanvas *c1 = new TCanvas("c1", "Canvas Title", 20, 20);
    TH3F *h3box = new TH3F("h3box", "", 9, -4, 4, 9, -4, 4, 9, -4, 4);
    
    float x, y, z;
    
    for (int i = -1; i < 2; i++){
        x = (float)i;
        y = (float)(3 * i);
        z = 1.5 / (float)(i + 2);

        h3box->Fill(x, y, z);
    }
    
    //=====================================================
    
    h3box->GetXaxis()->SetTitle("x");
    h3box->GetYaxis()->SetTitle("y");
    h3box->GetZaxis()->SetTitle("z");
    
    //int nl = h3box->GetXaxis()->GetNlabels(); // => error: no member named 'GetNlabels' in 'TAxis'
    
    int nDivisions = 4;
    h3box->GetXaxis()->SetNdivisions(nDivisions);
    
    for (int i = 0; i < nDivisions; i++)
        h3box->GetXaxis()->ChangeLabel(i, -1, 0.095, 2, -1, -1, ""); // => Affects just first 3 labels
        //h3box->GetXaxis()->ChangeLabel(i, -1, 0.05, 2, -1, -1, ""); // => Default font size
        //h3box->GetXaxis()->ChangeLabel(i, -1, 0.025, 2, -1, -1, ""); // => First 3 labels disappear
    
    h3box->Draw("BOX");
    
    c1->SaveAs("result.pdf");
    
    //=====================================================
    
    delete h3box;
    delete c1;
}

Result:
result-0.pdf (14.3 KB)

To tune the labels I have tried to use ChangeLabel function, but didn’t succeed. Also a try to use GetNlabels() leads to an error:

error: no member named 'GetNlabels' in 'TAxis'

Could anyone help me to obtain satisfactory tick labels, please? Thank you.

My system (under podman toolbox):

$ lsb_release -d && uname -r
Description:    CentOS Linux release 7.9.2009 (Core)
6.3.0-2-amd64

$ g++ --version
g++ (GCC) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ root --version
ROOT Version: 6.26/10
Built for linuxx8664gcc on Dec 14 2022, 15:23:00
From v6-26-10@v6-26-10

Dear @Natalia_D ,

Thanks for reporting, indeed the labeling looks ugly.

@couet can you please help out here?

Cheers,
Vincenzo

The link doesn’t work anymore (it has expired), so I cannot see what you get and it’s not clear what you mean by “the ‘-’ sign coincides with the digits for negative numbers”.
Anyway, adjusting your code like this:

{
  TCanvas *c1 = new TCanvas("c1", "Canvas Title");
  TH3F *h3box = new TH3F("h3box", "", 9, -4, 4, 9, -4, 4, 9, -4, 4);

  float x, y, z;

  for (int i = -1; i < 2; i++){
    x = (float)i;
    y = (float)(3 * i);
    z = 1.5 / (float)(i + 2);

    h3box->Fill(x, y, z);
  }

  //=====================================================

  h3box->GetXaxis()->SetTitle("x");
  h3box->GetYaxis()->SetTitle("y");
  h3box->GetZaxis()->SetTitle("z");

  //int nl = h3box->GetXaxis()->GetNlabels(); // => error: no member named 'GetNlabels' in 'TAxis'

  int nDivisions = 4;
  h3box->GetXaxis()->SetNdivisions(nDivisions);

  for (int i = 1; i <= nDivisions+1; i++)
    h3box->GetXaxis()->ChangeLabel(i, -1, -1, 2, -1, -1, ""); // => Affects just first 3 labels
  //h3box->GetXaxis()->ChangeLabel(i, -1, 0.05, 2, -1, -1, ""); // => Default font size
  //h3box->GetXaxis()->ChangeLabel(i, -1, 0.025, 2, -1, -1, ""); // => First 3 labels disappear

  h3box->GetXaxis()->SetLabelSize(0.04);
  h3box->GetXaxis()->SetLabelOffset(-0.01);
  h3box->GetXaxis()->SetTitleOffset(1.2);
  h3box->Draw("BOX");

  //c1->SaveAs("result.pdf");

  //=====================================================

  //delete h3box;
  //delete c1;
}

I get

Note:

  • the correction in the loop variable limits for ChangeLabel;
  • the use of SetLabelSize, SetLabelOffset and SetTitleOffset (there’s also SetTitleSize if you need); play around with the values to match your needs;
  • to use GetNlabels(), you need ROOT v6.28, it is not included in previous versions.

Dear Vincenzo and @dastudillo,

Thank you for your replies!

I have commented out the loop, containing ChangeLabel, and added three lines with SetLabelSize, SetLabelOffset, and SetTitleOffset. This is the result:

It is still bad…

When I try to change the SetLabelSize, it increases by jump after the value 0.09, and disappears for the values < 0.03. For the values between them, the size doesn’t change.

If I don’t comment out the loop, the size of the first three labels is large.

to use GetNlabels(), you need ROOT v6.28, it is not included in previous versions.

Clear, thank you!

// I have updated the image in the original message.

Regards,
Natalia

Are you doing anything else in the code? Why do you seem to have 2 rows of labels on y? If you do exactly what I posted, do you get anything different from my picture?
I don’t see what you say, so it’s better if you post the exact code that results in your last image. Here is a test using label sizes < 0.03, >0.09, and in between, where I don’t see what you say (<0.03 does not disappear, …). I used SetNdivisions on all axes to avoid bad overlaps (you should not use very large fonts with so many labels, you have to adjust and make it look fine).

 {
  TCanvas *c1 = new TCanvas("c1", "Canvas Title");
  TH3F *h3box = new TH3F("h3box", "", 9, -4, 4, 9, -4, 4, 9, -4, 4);

  float x, y, z;
  for (int i = -1; i < 2; i++){
    x = (float)i;
    y = (float)(3 * i);
    z = 1.5 / (float)(i + 2);
    h3box->Fill(x, y, z);
  }

  h3box->GetXaxis()->SetTitle("x");
  h3box->GetYaxis()->SetTitle("y");
  h3box->GetZaxis()->SetTitle("z");

  //int nl = h3box->GetXaxis()->GetNlabels(); // => error: no member named 'GetNlabels' in 'TAxis'

  int nDivisions = 4;
  h3box->GetXaxis()->SetNdivisions(nDivisions);
  h3box->GetYaxis()->SetNdivisions(nDivisions);
  h3box->GetZaxis()->SetNdivisions(nDivisions);

  h3box->GetXaxis()->SetLabelSize(0.10);
  h3box->GetYaxis()->SetLabelSize(0.05);
  h3box->GetZaxis()->SetLabelSize(0.02);

  h3box->GetXaxis()->SetLabelOffset(0.01);
  h3box->GetYaxis()->SetLabelOffset(0.01);
  h3box->GetZaxis()->SetLabelOffset(0.01);

  h3box->GetXaxis()->SetTitleOffset(1.2);
  h3box->GetYaxis()->SetTitleOffset(1.2);
  h3box->GetZaxis()->SetTitleOffset(1.2);

  h3box->Draw("BOX");
}

The execution of the code from your last post gives the same result as yours.

I just have realized, that the key difference is in specifying the window size at the creation of the TCanvas. As only I remove this, everything becomes good.
The rest of differences are redirecting output into PDF file and deleting h2box and c1.

It turns out, that specifying large enough values for window size leads to the normal picture…
The code and result are below.

#include <TAxis.h>

void uglyTLabels(){
    //TCanvas *c1 = new TCanvas("c1", "Canvas Title"); // => good tick labels
    //TCanvas *c1 = new TCanvas("c1", "Canvas Title", 20, 20); // => bad tick labels
    TCanvas *c1 = new TCanvas("c1", "Canvas Title", 2000, 2200); // => good tick labels

    TH3F *h3box = new TH3F("h3box", "", 9, -4, 4, 9, -4, 4, 9, -4, 4);
    
    float x, y, z;
    for (int i = -1; i < 2; i++){
        x = (float)i;
        y = (float)(3 * i);
        z = 1.5 / (float)(i + 2);
        h3box->Fill(x, y, z);
    }
    
    //=====================================================
    
    h3box->GetXaxis()->SetTitle("x");
    h3box->GetYaxis()->SetTitle("y");
    h3box->GetZaxis()->SetTitle("z");
    
    int nDivisions = 4;
    h3box->GetXaxis()->SetNdivisions(nDivisions);
    h3box->GetYaxis()->SetNdivisions(nDivisions);
    h3box->GetZaxis()->SetNdivisions(nDivisions);
    
    h3box->GetXaxis()->SetLabelSize(0.10);
    h3box->GetYaxis()->SetLabelSize(0.05);
    h3box->GetZaxis()->SetLabelSize(0.02);
    
    h3box->GetXaxis()->SetLabelOffset(0.01);
    h3box->GetYaxis()->SetLabelOffset(0.01);
    h3box->GetZaxis()->SetLabelOffset(0.01);
    
    h3box->GetXaxis()->SetTitleOffset(1.2);
    h3box->GetYaxis()->SetTitleOffset(1.2);
    h3box->GetZaxis()->SetTitleOffset(1.2);
    
    h3box->Draw("BOX");
    
    c1->SaveAs("result.pdf");
    
    //=====================================================
    
    delete h3box;
    delete c1;
}

As for me, this is strange, but anyway the solution is found:
to fix out the ugly tick labels like in my case, one should remove the specifying of the window sizes at the creation of the TCanvas or set them large enough.

Thanks you, @dastudillo, for the help!

PS
When I execute the code with TCanvas *c1 = new TCanvas("c1", "Canvas Title", 2000, 2200);, there is some white splash over my screen before update of the PDF file. I presume, this is some default viewer, which at lower window sizes dies too early to be seen. Does anyone know, how to switch off it?
[I understand, this is a little offtopic, but I would be very thankful, if someone helped me here. I believe, the answer is not long.]

Hi @Natalia_D,

    TCanvas *c1 = new TCanvas("c1", "Canvas Title", 20, 20); // => bad tick labels

This code creates TCanvas with the pixel size 20x20.
With such small dimensions algorithm for fonts scaling and labels positioning does not work properly.
One should use more realistic values like 500x500 or 1000x1000.

Regards,
Sergey

Thank you, Sergey @linev, for the reply!

In fact, we have arrived to this. But anyway, thank you for the clarification!

Regards,
Natalia