PixelToY function not giving sensible results

Dear ROOT experts,

I have just found that the PIxelToY function is not working as I expect it to do.

Let’s say I have a histogram and multiple labels that I want to plot. Once I plot everything, I want the histogram to not overlap with the labels. For this, I get the latex labels NDC coordinate, the maximum of the histogram in USER coordinates, and now I want to compare the values. The result of this is to have:

maxy_histogram < miny_labels

Since we have different coordinates systems, I want to transform the NDC coordinates to USER ones. For this I do like the following:

miny_label_user = ROOT.gPad.PixeltoY(ROOT.gPad.VtoPixel(miny_label_ndc) - int(ROOT.gPad.GetWh() * ROOT.gPad.GetHNDC()))

as following the instructions here.

However, in this minimal example, you can see that the output of miny_label_user doesn’t make sense at all, being really similar to the original miny_label_ndc value.

import ROOT

# Create the canvas
canvas = ROOT.TCanvas("canvas", "My Canvas")

# Create the histogram
histogram = ROOT.TH1F("histogram", "My Histogram; X Label; Y Label", 50, -5, 5)

# Fill the histogram with some random data
for i in range(1000):

# Draw the histogram

# Set the label properties
label = ROOT.TLatex()

# Draw the labels
label.DrawLatexNDC(0.2, 0.9, "My Histogram Title")
label.DrawLatexNDC(0.2, 0.85, "X Axis Label")
label.DrawLatexNDC(0.7, 0.85, "Y Axis Label")

miny_label_ndc = 0.85

max_histogram_user = histogram.GetMaximum()

print(f'{miny_label_ndc = }')

print(f'Conversion miny_label_ndc to USER coordinates = {ROOT.gPad.PixeltoY(ROOT.gPad.VtoPixel(miny_label_ndc) - int(ROOT.gPad.GetWh()*ROOT.gPad.GetHNDC()))}')
print(f'{max_histogram_user = }')

# Update the canvas

# Save the histogram as a PDF file

The output is:

miny_label_ndc = 0.85
Conversion miny_label_ndc to USER coordinates = 0.8505263157894737
max_histogram_user = 89.0

and by inspecting the canvas, the NDC coordinate should be approximatelly 81.9

How can I obtain the USER value of the coordinates correctly, starting from NDC values?

Thanks a lot for the help,

test.py (1.0 KB)

ROOT Version: 6.28

ROOT.gPad.Modified() ; ROOT.gPad.Update() ;
print(f'Conversion miny_label_ndc to USER coordinates = {ROOT.gPad.PixeltoY(ROOT.gPad.VtoPixel(miny_label_ndc) - ROOT.gPad.GetWh())}')

worked perfectly. Thank you very very much.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.