Pablo
Pablo

Reputation: 167

Seaborn: How to specify plot minor ticks and gridlines in all plots when using lmplot?

I'm trying to get minor ticks and gridlines plotted in all plots when using lmplot, using the code below:

sns.set(context="notebook", style='white', font_scale=2)

g=sns.lmplot(data=data ,
             x="x", 
             y="y",
             col="item",   # or use rows to display xplots in rows (one on top of the other)
             fit_reg=False,
             col_wrap=2,
             scatter_kws={'linewidths':1,'edgecolor':'black', 's':100}
            )

g.set(xscale='linear', yscale='log', xlim=(0,0.4), ylim=(0.01, 10000))

for ax in g.axes.flatten():
    ax.tick_params(axis='y', which='both', direction='out', length=4, left=True)
    ax.grid(b=True, which='both', color='gray', linewidth=0.1)
    
for axis in [ax.yaxis, ax.xaxis]:
    formatter = FuncFormatter(lambda y, _: '{:.16g}'.format(y))
    axis.set_major_formatter(formatter)

sns.despine()

g.tight_layout()

# Show the results
plt.show()

So far, only major ticks and gridlines are shwon in all plots.

Thanks for any advice for solving this

Upvotes: 0

Views: 3677

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

Your code work fine for me. I think the problem is that when the major labels are too big, matplotlib chooses not to display the minor ticks, and hence the minor grid lines. You may either change font_scale, or increase the size of your figure (see height= in lmplot())

Consider the following code with font_scale=1

tips = sns.load_dataset('tips')

sns.set(context="notebook", style='white', font_scale=1)
g = sns.lmplot(x="total_bill", y="tip", col="day", hue="day",
               data=tips, col_wrap=2, height=3)

g.set(xscale='linear', yscale='log')
for ax in g.axes.flatten():
    ax.tick_params(axis='y', which='both', direction='out', length=4, left=True)
    ax.grid(b=True, which='both', color='gray', linewidth=0.1)
    
for axis in [ax.yaxis, ax.xaxis]:
    formatter = matplotlib.ticker.FuncFormatter(lambda y, _: '{:.16g}'.format(y))
    axis.set_major_formatter(formatter)

sns.despine()

g.tight_layout()

# Show the results
plt.show()

enter image description here

compare with the result using font_scale=2

enter image description here

Upvotes: 2

Related Questions