2013-07-02 7 views
14

매트 플롯의 전경에 산란 플롯을 가져 오는 방법을 아는 사람이 있습니까? 윤곽 상단에 그려진 흩뿌 림을 표시해야하지만 기본적으로 아래에 그려집니다.Matplotlib : 윤곽 플롯 상단에서 전경으로 자르는 플롯

미리 감사드립니다!

+4

체크 아웃 'scatter' 방법의 ['zorder' (http://matplotlib.org/api/artist_api.html#matplotlib.artist.Artist.set_zorder) 파라미터. [예제 사용법] (http://matplotlib.org/examples/pylab_examples/zorder_demo.html)을 matplotlib 사이트에서 가져옵니다. – hooy

+0

그게 고마워! – Mike

답변

23

zorder 매개 변수를 사용하여 다른 플롯을 표시 할 순서를 수동으로 선택할 수 있습니다. scatter 방법.

아래의 코드에서 왼쪽 서브 그림의 산점도는 zorder=1이고 오른쪽 하위 그림은 zorder=-1입니다. 가장 높은 숫자가 인 개체가 위에 놓입니다. 즉, 첫 번째 서브 플롯의 윤곽 상단에 산포가 배치되고 두 번째 서브 플롯의 아래쪽에 산포가 배치됩니다.

import numpy as np 
import matplotlib.cm as cm 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 

delta = 0.025 
x = np.arange(-3.0, 3.0, delta) 
y = np.arange(-2.0, 2.0, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
Z = 10.0 * (Z2 - Z1) 

norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max()) 
cmap = cm.PRGn 

levels = np.arange(-2.0, 1.601, 0.4) 

fig, axes = plt.subplots(1,2, sharey=True) 

for ax, zord in zip(axes, [1, -1]): 
    ax.contourf(X, Y, Z, levels, 
       cmap=cm.get_cmap(cmap, len(levels)-1), 
       norm=norm) 
    ax.autoscale(False) # To avoid that the scatter changes limits 
    ax.scatter(np.random.uniform(-3,3,10), 
       np.random.uniform(-2,2,10), 
       zorder=zord) 
    ax.set_title('Scatter with zorder={0}'.format(zord)) 

enter image description here