2013-10-25 2 views
1

matplotlib와 함께 python을 사용하여 아래의 스케치를 그려 보겠습니다. 그리드를 그리는 것이 그리 어렵지는 않지만 특정 사각형을 특정 방법으로 채색하는 것은 어떨까요?matplotlib의 격자 패턴 그리기

답변

3
N = 15 
# make an empty data set 
data = np.ones((N, N)) * np.nan 
# fill in some fake data 
for j in range(3)[::-1]: 
    data[N//2 - j : N//2 + j +1, N//2 - j : N//2 + j +1] = j 
# make a figure + axes 
fig, ax = plt.subplots(1, 1, tight_layout=True) 
# make color map 
my_cmap = matplotlib.colors.ListedColormap(['r', 'g', 'b']) 
# set the 'bad' values (nan) to be white and transparent 
my_cmap.set_bad(color='w', alpha=0) 
# draw the grid 
for x in range(N + 1): 
    ax.axhline(x, lw=2, color='k', zorder=5) 
    ax.axvline(x, lw=2, color='k', zorder=5) 
# draw the boxes 
ax.imshow(data, interpolation='none', cmap=my_cmap, extent=[0, N, 0, N], zorder=0) 
# turn off the axis labels 
ax.axis('off') 

example output

+0

TNX squares,하지만 난이 얻을 : __init의 __()는 그냥 그 제거 –

+0

@AndreiBerceanu 'tight_layout'예기치 않은 키워드 인수를 얻었다. 그것은 최신 버전의 mpl – tacaswell

+0

에서 작동합니다. matplotlib를 1.4로 업데이트했고 tight_layout 오류를 제거했습니다. 그러나 이제 경고 메시지가 표시됩니다.이 그림에는 tight_layout과 호환되지 않는 축이 포함되어 있으므로 그 결과가 정확하지 않을 수 있습니다. –