2014-02-21 69 views
16

我是使用Matplotlib製作漂亮圖的Seaborn包的粉絲。但我似乎無法弄清楚如何在我的情節中顯示較小的網格線。使用seaborn添加次要網格到matplotlib圖

import numpy as np 
import matplotlib.pyplot as plt 
import seaborn as sbn 

x = np.linspace(0, 2 * np.pi, 100) 
y = np.sin(x) 

fig, ax = plt.subplots(1, 1) 
ax.scatter(x, y) 

ax.grid(b=True, which='major') 
ax.grid(b=True, which='minor') 

給出:

enter image description here

這裏有什麼想法?還有關於如何調整顯示的Seaborn網格線的任何想法......特別是,我想讓它們變得更窄。

回答

20

傷口與tcaswell的提示聯合CT朱的回答是:

import numpy as np 
import matplotlib as mpl 
import matplotlib.pyplot as plt 
import seaborn as sbn 

x = np.linspace(0, 2 * np.pi, 100) 
y = np.sin(x) 

fig, ax = plt.subplots(1, 1) 

ax.scatter(x, y) 
ax.get_xaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) 
ax.get_yaxis().set_minor_locator(mpl.ticker.AutoMinorLocator()) 
ax.grid(b=True, which='major', color='w', linewidth=1.0) 
ax.grid(b=True, which='minor', color='w', linewidth=0.5) 

enter image description here

+3

你可以在seaborn github上打開一個問題嗎?您不需要自己設計小網格線,但我認爲這是當前代碼中的一個疏忽。 – mwaskom

6

這是因爲輕微的蜱尚未確定,所以我們需要添加例如:

ax.set_xticks(np.arange(0,8)-0.5, minor=True) 
ax.set_yticks([-1.25, -0.75, -0.25,0.24,0.75,1.25], minor=True) 

enter image description here

+5

'ax.get_xaxis()。set_minor_locat或(...)' – tacaswell