2017-03-04 2 views
1

두 그룹의 2 차원 분포를 비교해야합니다.Matplotlib의 오버레이 윤곽 플롯

matplotlib.pyplot.contourf을 사용하고 플롯을 오버레이하면 각 등고선 플롯의 배경색이 전체 플롯 공간을 채 웁니다. 각 등고선의 중심을보다 쉽게 ​​볼 수 있도록 모든 등고선 플롯에 대해 최저 윤곽선 레벨을 투명하게 만드는 방법이 있습니까?

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import cm 
import scipy.stats as st 

def make_cloud(x, y, std, n=100): 
    x = np.random.normal(x, std, n) 
    y = np.random.normal(y, std, n) 
    return np.array(zip(x, y)) 

def contour_cloud(x, y, cmap): 
    xmin, xmax = -4, 4 
    ymin, ymax = -4, 4 

    xx, yy = np.mgrid[xmin:xmax:100j, ymin:ymax:100j] 
    positions = np.vstack([xx.ravel(), yy.ravel()]) 
    values = np.vstack([x, y]) 
    kernel = st.gaussian_kde(values) 
    f = np.reshape(kernel(positions).T, xx.shape) 

    plt.contourf(xx, yy, f, cmap=cmap, alpha=0.5) 


cloud1 = make_cloud(-1, 1, 1) 
cloud2 = make_cloud(1, -1, 1) 

plt.scatter(x=cloud1[:,0], y=cloud1[:,1]) 
plt.scatter(x=cloud2[:,0], y=cloud2[:,1], color='red') 

fig = plt.gcf() 
ax = plt.gca() 

contour_cloud(x=cloud1[:, 0], y=cloud1[:, 1], cmap=cm.Blues) 
contour_cloud(x=cloud2[:, 0], y=cloud2[:, 1], cmap=cm.Reds) 

enter image description here

답변

1

contourf에 대해보고하기를 원할 것입니다 몇 가지 컨트롤이 있습니다. 다른 레벨을 수동으로 변경할 수 있으며 색상 맵을 초과/미만으로 변경할 수 있습니다. 기본적으로 가장 낮은 레벨 (또는 최대 값) 아래 영역의 채우기는 투명하게 보입니다.

그래서, 당신이 원하는 것을 할 수있는 가장 쉬운 방법은 수동으로 수준을 지정하고 가장 낮은 수준 아래 지점이 그들은 지정하는 것입니다 만, 가장 높은 수준의 위 어떤 점 수 없습니다.

당신은 교체하는 경우 :

plt.contourf(xx, yy, f, cmap=cmap, alpha=0.5) 

로 :

step = 0.02 
m = np.amax(f) 
levels = np.arange(0.0, m, step) + step 
plt.contourf(xx, yy, f, levels, cmap=cmap, alpha=0.5) 

이 같은 이미지를 생성합니다 이상/색상 맵 값에 따라 값의 동작에 대한 자세한 내용은 enter image description here

, here을 참조하십시오.

+0

감사합니다. – Chris

관련 문제