簡體   English   中英

Seaborn:如何在使用 lmplot 時在所有繪圖中指定繪圖小刻度和網格線?

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

使用 lmplot 時,我正在嘗試使用以下代碼在所有圖中繪制小刻度和網格線:

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

到目前為止,所有圖中只顯示主要刻度線和網格線。

感謝您提供解決此問題的任何建議

你的代碼對我來說很好。 我認為問題是當主要標簽太大時,matplotlib 選擇不顯示次要刻度,因此不顯示次要網格線。 您可以更改font_scale ,或增加圖形的大小(請參閱lmplot() height=

考慮以下帶有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()

在此處輸入圖片說明

與使用font_scale=2的結果進行比較

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM