Issue with setting Times New Roman font in PyROOT plots


I’m trying to create plots with the Times New Roman font using PyROOT for my histograms. According to the official ROOT documentation on fonts (sorry, I cannot put the link because I am new to the forum), Times New Roman corresponds to font code 13. However, when I apply this font code, the axis labels are not displayed correctly, as shown in the screenshot below:

As a workaround, I’ve used font_code = 2, but this is not the desired font. It’s important to mention that not all the fonts listed in the documentation seem to work—another example is Helvetica-BoldOblique (font code 7), which also doesn’t display correctly, similar to Times New Roman.

Here’s another screenshot showing the result with a font code of 2:

Below I provide the code.

#=========================================================================================================================
font_code = 13
canvas5 = ROOT.TCanvas("canvas5", "Histograms", 800, 600)
canvas5.cd()
# Histogram of DeltaQ CCD -DeltaQ Keithley
DeltaDeltaQ=np.array(DeltaQ_values_keithley)-np.array(DeltaQ_values_CCD);
hist_DeltaDeltaQ = ROOT.TH1F("hist_DeltaDeltaQ", "", 20, np.min(DeltaDeltaQ), np.max(DeltaDeltaQ))
for value in DeltaDeltaQ:
    hist_DeltaDeltaQ.Fill(value)
hist_DeltaDeltaQ.Draw()
hist_DeltaDeltaQ.GetXaxis().SetTitle("Charge difference (C)")
hist_DeltaDeltaQ.GetXaxis().SetLabelFont(font_code); hist_DeltaDeltaQ.GetXaxis().SetTitleFont(font_code)


hist_DeltaDeltaQ.GetYaxis().SetTitle("Frequency")
hist_DeltaDeltaQ.GetYaxis().SetLabelFont(font_code); hist_DeltaDeltaQ.GetYaxis().SetTitleFont(font_code)

hist_DeltaDeltaQ.SetFillColor(ROOT.kRed); hist_DeltaDeltaQ.SetLineColorAlpha(ROOT.kRed,1)
hist_DeltaDeltaQ.SetStats(False)
canvas5.Update()

# Save the histogram of DeltaQ to the same PDF using matplotlib's PdfPages
canvas5.Print('delta_charge_plot.eps') #and close pdf
#=========================================================================================================================

ROOT Version: 6.32.00
Platform: linuxx8664gcc
Compiler: GCC


Hi @mgamero,

thank you for your question. I believe @couet will be able to help you here.

Cheers,
Marta

You need to concatenate de font precision [1] next to the font number. So font_code = 132

[1] ROOT: TAttText Class Reference

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