简体   繁体   中英

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

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()

在此处输入图片说明

compare with the result using font_scale=2

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM