X-axis labels vanish when axis is log-scaled

Dear ROOT community, I am plotting a mass variable. Upon setting the x-axis to log scale the axis labels vanish. My plotting code (slightly long, but hopefully clear enough) is below. To be clear, this only happens for var == h_mHat. Any help would be deeply appreciated.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar  2 12:01:01 2021

@author: rbrener
"""

import numpy as np
from matplotlib import pyplot as plt
import sys,math
import os

import matplotlib as mpl
sys.path = ["/home/rbrener/Programs/root_v6.18.04/lib/"] + sys.path
sys.path = ["/home/rbrener/Programs/root_6_install/lib"]+sys.path

import ROOT
from ROOT import *


from ROOT import TFile, TH1D, TH2D, TCanvas, TTree, TLorentzVector, TMath

from matplotlib.font_manager import FontProperties
from matplotlib.ticker import (MultipleLocator, MaxNLocator, FuncFormatter,FormatStrFormatter, AutoMinorLocator, ScalarFormatter)


#Some cross-sections:
xs_QCD = 2.362e-04
xs_NP = 2.803e-04
L_i = 1
N_E = 1e6
w_QCD = L_i/(N_E/xs_QCD)
w_NP  = L_i/(N_E/xs_NP)


def GetGoodHistosLimits(histos_list):
  histo_minbin_dict = {}
  histo_maxbin_dict = {}
  ymin = 0
  ymax = 0
  for histo in histos_list:
    bin_content_list = []
    for ibin in range(histo.GetNbinsX()):
      bin_content_list.append(histo.GetBinContent(ibin))

    min_bin = bin_content_list.index(min([el for el in bin_content_list if el > 0]))
    max_bin = bin_content_list.index(max([el for el in bin_content_list if el > 0]))

    histo_minbin_dict[histo]=histo.GetBinContent(min_bin)
    histo_maxbin_dict[histo]=histo.GetBinContent(max_bin)

  ymin = histo_minbin_dict[min(histo_minbin_dict)]
  ymax = histo_maxbin_dict[max(histo_maxbin_dict)]

  return ymin, ymax


def createRatio(h1, h2):
    h3 = h1.Clone("h3")
    h3.SetLineColor(kBlack)
    h3.SetMarkerStyle(21)
    h3.SetTitle("")
    h3.SetMinimum(0.2)
    h3.SetMaximum(4.0)
    # Set up plot for markers and errors
    h3.Sumw2()
    h3.SetStats(0)
    h3.Add(h2,-1)
    h3.Divide(h2)

    # Adjust y-axis settings
    y = h3.GetYaxis()
    y.SetTitle("(CI #minus QCD)/QCD")
    y.SetNdivisions(505)
    y.SetTitleSize(20)
    y.SetTitleFont(43)
    y.SetTitleOffset(1.55)
    y.SetLabelFont(43)
    y.SetLabelSize(18)
    y.SetRangeUser(-0.75,0.75)

    # Adjust x-axis settings
    x = h3.GetXaxis()
    x.SetTitleSize(20)
    x.SetTitleFont(43)
    x.SetTitleOffset(4.0)
    x.SetLabelFont(43)
    x.SetLabelSize(18)

    return h3

def createCanvasPads():
    c = TCanvas("c", "canvas", 800, 800)
    # Upper histogram plot is pad1
    pad1 = TPad("pad1", "pad1", 0, 0.3, 1, 1.0)
    pad1.SetBottomMargin(0.03)
    #pad1.SetLogy()
    pad1.Draw()
    # Lower ratio plot is pad2
    c.cd()  # returns to main canvas before defining pad2
    pad2 = TPad("pad2", "pad2", 0, 0.05, 1, 0.3)
    pad2.SetTopMargin(0.03)
    pad2.SetBottomMargin(0.25)
    pad2.Draw()

    TexList = []

    TexList.append(TLatex(0.55,0.85,"Pythia8"))
    TexList[0].SetNDC()
    TexList[0].SetTextAlign(13)
    TexList[0].SetTextFont(72) # helvetica italic
    TexList[0].SetTextSize(0.05)
    TexList[0].SetLineWidth(1)

    TexList.append(TLatex(0.68,0.85,"Simulation"))
    TexList[1].SetNDC()
    TexList[1].SetTextAlign(13)
    TexList[1].SetTextFont(42)
    TexList[1].SetTextSize(0.05)
    TexList[1].SetLineWidth(1)

    TexList.append(TLatex(0.83,0.70,"#sqrt{s}=13 TeV, #Lambda=22 TeV,"))
    TexList[2].SetNDC()
    TexList[2].SetTextAlign(31)
    TexList[2].SetTextFont(42)
    TexList[2].SetTextSize(0.037)
    TexList[2].SetLineWidth(2)

    TexList.append(TLatex(0.78,0.66,"#eta_{LL}=-1, #eta_{RR}=#eta_{LR}=0"))
    TexList[3].SetNDC()
    TexList[3].SetTextAlign(31)
    TexList[3].SetTextFont(42)
    TexList[3].SetTextSize(0.037)
    TexList[3].SetLineWidth(2)


    return c, pad1, pad2, TexList


legend = TLegend()


HQCD = TFile("/home/rbrener/Analysis/ContactInteractions/HQCD_analysis.root")
HQCD_and_qq2qq_and_qqbar2qqbar = TFile("/home/rbrener/Analysis/ContactInteractions/HQCD_and_qq2qq_and_qqbar2qqbar_analysis.root")


def getHists():

    hist_dict = {}

    for hist in HQCD.GetListOfKeys():
        hist_list = []

        hName = hist.GetName()
        hNameStr = str(hName)

        histTH1_HQCD = HQCD.Get(hName)

        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar = HQCD_and_qq2qq_and_qqbar2qqbar.Get(hName)


        histTH1_HQCD.SetLineColor(kBlack)
        histTH1_HQCD.SetLineWidth(2)
        histTH1_HQCD.Scale(w_QCD)

        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.SetLineColor(kRed+1)
        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.SetLineStyle(2)
        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.SetLineWidth(2)
        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.Scale(w_NP)


        hist_list.extend([histTH1_HQCD, histTH1_HQCD_and_qq2qq_and_qqbar2qqbar])

        hist_dict[hName] = hist_list


    return hist_dict


gStyle.SetOptStat(0)
gStyle.SetOptTitle(0)
gROOT.SetBatch()


def ratioplot():
    # create required parts

    hist_dict = getHists()

    for var in hist_dict:


        histTH1_HQCD, histTH1_HQCD_and_qq2qq_and_qqbar2qqbar = hist_dict[var]
        #histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio = createRatio(histTH1_HQCD_and_qq2qq_and_qqbar2qqbar, histTH1_HQCD)

        c, pad1, pad2, TexList = createCanvasPads()

        if "Chi" in var and len(var) > 5:
            if "inf" in var:
                TexList.append(TLatex(0.78,0.56,"#hat{m} > 5.4 TeV"))

            elif "Chi_mHat" in var:
                mass_min = str(float(var[11:13])/10)
                mass_max = str(float(var[14:16])/10)
                mass_range = mass_min + " < #hat{m} < " + mass_max + " TeV"
                TexList.append(TLatex(0.78,0.56,mass_range))

            HQCD_Int = histTH1_HQCD.Integral()
            NP_Int   = histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.Integral()
            TexList.append(TLatex(0.83,0.48, "#int #chi(QCD)d#chi = " + str(round(HQCD_Int, 4)) + ", "))
            TexList.append(TLatex(0.85,0.38, "#int #chi(QCD+CI)d#chi = " + str(round(NP_Int, 4))))
#            histTH1_HQCD.Scale(1/HQCD_Int)
#            histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.Scale(1/NP_Int)

#            for j in range(histTH1_HQCD.GetNbinsX()):
#                histTH1_HQCD.SetBinContent(j, histTH1_HQCD.GetBinContent(j)/histTH1_HQCD.GetBinWidth(j) )
#                histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.SetBinContent(j, histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetBinContent(j)/histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetBinWidth(j) )

            histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio = createRatio(histTH1_HQCD_and_qq2qq_and_qqbar2qqbar, histTH1_HQCD)

            TexList[4].SetNDC()
            TexList[4].SetTextAlign(31)
            TexList[4].SetTextFont(42)
            TexList[4].SetTextSize(0.037)
            TexList[4].SetLineWidth(2)

            TexList[5].SetNDC()
            TexList[5].SetTextAlign(31)
            TexList[5].SetTextFont(42)
            TexList[5].SetTextSize(0.037)
            TexList[5].SetLineWidth(2)

            TexList[6].SetNDC()
            TexList[6].SetTextAlign(31)
            TexList[6].SetTextFont(42)
            TexList[6].SetTextSize(0.037)
            TexList[6].SetLineWidth(2)

            l = TMathText()
            #histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetTitle("1/N dN/d#chi")
            histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetTitle("1/(dN/d#sigma)")
            #histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetXaxis().SetTitle("#chi")
            #pad2.SetLogx(1)
            #pad1.SetLogx(1)


        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio = createRatio(histTH1_HQCD_and_qq2qq_and_qqbar2qqbar, histTH1_HQCD)

        histo_list = []
        histo_list.extend([histTH1_HQCD,histTH1_HQCD_and_qq2qq_and_qqbar2qqbar])
        ymin = GetGoodHistosLimits(histo_list)[0]
        ymax = GetGoodHistosLimits(histo_list)[1]

        if var == 'h_mHat':
            print ("h_mHat")
            #pad2.SetLogx(1)
            #pad1.SetLogx(1)
            #histTH1_HQCD.GetYaxis().SetRangeUser(histTH1_HQCD.GetMaximum()*0.005,histTH1_HQCD.GetMaximum()*3.5)
            histTH1_HQCD.GetXaxis().SetRangeUser(1100,8000)
            histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio.GetXaxis().SetRangeUser(1100,8000)
            #histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetXaxis().SetNdivisions(2)

        if 'hChi' in var:
            histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetRangeUser(histTH1_HQCD.GetMaximum()*0.0001,histTH1_HQCD.GetMaximum()*10)
            histTH1_HQCD.GetYaxis().SetRangeUser(histTH1_HQCD.GetMaximum()*0.0001,histTH1_HQCD.GetMaximum()*10)
            histTH1_HQCD.GetXaxis().SetRangeUser(1,30)
            histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio.GetXaxis().SetRangeUser(1,30)

        if 'h_mHat' in var:
            histTH1_HQCD.GetYaxis().SetRangeUser(histTH1_HQCD.GetMaximum()*0.005,histTH1_HQCD.GetMaximum()*3.5)
            histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetNdivisions(10)
            pad1.SetLogy(1)
            #pad2.SetLogx(1)
            if var == 'h_mHat_54_inf':
                histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.Rebin(3)
                histTH1_HQCD.Rebin(3)
                histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio.Rebin(3)
                histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio.GetYaxis().SetRangeUser(0,10)

        else:
#            histTH1_HQCD.GetYaxis().SetRangeUser(0.001*ymin, 3.5*ymax)
            histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetRangeUser(0.001*ymin, 2.0*ymax)

        legend = TLegend(0.55, 0.48, 0.85, 0.53)

        legend.AddEntry(histTH1_HQCD, "QCD", "l")
        legend.AddEntry(histTH1_HQCD_and_qq2qq_and_qqbar2qqbar, "QCD + CI: qq #rightarrow qq, q#bar{q} #rightarrow q#bar{q}", "l")


        legend.SetBorderSize(0)
        legend.SetTextAlign(11)
        legend.SetFillStyle(0)
        legend.Draw()

        # draw everything
        pad1.cd()
        gPad.SetTickx()
        gPad.SetTicky()
        #gStyle.SetTickLength(0.02,"y")

        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetTickLength(0.02)
        histTH1_HQCD.GetYaxis().SetTickLength(0.02)
        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.Draw("hist")

        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetTitleOffset(0)

        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetXaxis().SetLabelOffset(999)
        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetXaxis().SetLabelSize(0)
        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetXaxis().SetTitleSize(0)


        histTH1_HQCD.Draw("hist same")
        # to avoid clipping the bottom zero, redraw a small axis
        axis = TGaxis(-5, 20, -5, 220, 20, 220, 510, "")
        axis.SetLabelFont(43)
        axis.SetLabelSize(15)
        axis.Draw()

        #gPad.RedrawAxis()


        for tex in TexList:
            tex.Draw('same')

        pad2.cd()

        gPad.SetTickx()
        gPad.SetTicky()


        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio.SetLineColor(kRed+1)
        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio.SetLineWidth(2)
        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio.SetLineStyle(2)

        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetXaxis().SetLabelSize(25)

        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetXaxis().SetTickLength(0.02)


        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar_Ratio.Draw("hist")

        #gPad.RedrawAxis()

        histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetTitleOffset(1.5)
        #histTH1_HQCD_and_qq2qq_and_qqbar2qqbar.GetYaxis().SetTitleSize(2)

        c.SaveAs(var+".pdf")


ratioplot()

_ROOT Version:_6.22/06
_Platform:_Linux
Compiler: g++


Hi,

Try to use TAxis::SetMoreLogLabels() method.

Regards,
Sergey

Thanks so much Sergey!

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