2014-02-21 44 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