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

  • 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