Search code examples
python-3.xmatplotlibseabornaxisrelplot

How to set log scale minor ticks on xaxis for all relplot subplots after using .set


import pandas as pd
import seaborn as sns

tips = sns.load_dataset("tips")
tips['total_bill']=tips.total_bill*10**(tips.index/60+2)

sns.set(font_scale=2)
g=sns.relplot(data=tips,height=5, x="total_bill", y="tip", hue="day", col="time")
g.set(xscale='log')

According to different params (font_scale,height,etc), there might be some minor ticker for each 1/10 or not on the plt. enter image description here

How to make sure all minor tickers for each 1/10 be shown in all subplots?

like this:

enter image description here


Solution

    • Similar to this answer, except iterate through each axes.
    • This answer explains that seaborn .set() turns the ticks off by default, uses its darkgrid style, which modifies the matplotlib rc parameters, and sets the xtick.bottom and ytick.left parameters to False, however the options in the answer do not resolve the issue.
    • sns.set may be removed in future releases.
    • Tested in python 3.10, pandas 1.4.2, matplotlib 3.5.1, seaborn 0.11.2
    import matplotlib.ticker as mticker
    import seaborn as sns
    import numpy as np
    
    sns.set(font_scale=2)
    g = sns.relplot(data=tips, height=5, x="total_bill", y="tip", hue="day", col="time")
    g.set(xscale='log')
    
    # iterate through each axes
    for ax in g.axes.flat:
        ax.grid(True, which="both", axis='x', ls="--")
        locmin = mticker.LogLocator(base=10, subs=np.arange(0.1,1,0.1), numticks=10)  
        ax.xaxis.set_minor_locator(locmin)
    

    enter image description here

    With sns.set(font_scale=2, style='ticks')

    sns.set(font_scale=2, style='ticks') # or sns.set_theme(...)
    g = sns.relplot(data=tips, height=5, x="total_bill", y="tip", hue="day", col="time")
    g.set(xscale='log')
    
    for ax in g.axes.flat:
        locmin = mticker.LogLocator(base=10, subs=np.arange(0.1, 1, 0.1), numticks=10)  
        ax.xaxis.set_minor_locator(locmin)
    

    enter image description here

    Without Using .set

    enter image description here