2016-10-24 3 views
0

나는이 무작위로 값/색 '체스 판'을 만들어 루프의 값에 함수를 적용합니다. 예를 들어 배열의 모든 값을 증가하는 힘으로 올린 다음 모든 것을 다른 하위 플롯에 표시합니다.겹치는 텍스트

fig, axs = plt.subplots(2,2, figsize=(10, 10)) 
axs = axs.ravel() 

for i, n in zip(np.arange(4), np.arange(2,6)): 
    axs[i].imshow(np.power(rndm, n), cmap=my_cmap, interpolation = 'none') 
    axs[i].axis('off') 
    axs[i].text(1, 1, str(n), fontsize=14, color = 'y') 

및 결과 : 여기에 코드입니다

subplots

하지만 내가 좋아하는 정말 할 것은, 예를 들어, 4 개 색상 맵을 순환하는 것입니다

cmaps = ['viridis', 'inferno', 'plasma', 'magma'] 
fig, axs = plt.subplots(2,2, figsize=(10, 10)) 
axs = axs.ravel() 

for i, n in zip(np.arange(4), np.arange(2,6)): 
    axs[i].imshow(np.power(rndm, n), cmap=cmaps[i], interpolation = 'none') 
    axs[i].axis('off') 
    axs[i].text(1, 1, str(n), fontsize=14, color = 'y') 

coloured subplots

좋습니다. 그러나 여기에 내 코드가 깨지는 부분이 있습니다. 나는 이것을 개별 이미지로 저장하고 싶다. 텍스트 번호가 겹쳐 결국 것을 나는 사실을 제외하고 미세 인이 쓴 :

for i, n in zip(np.arange(4), np.arange(2,6)): 
    plt.imshow(np.power(rndm, n), cmap=cmaps[i], interpolation = 'none') 
    plt.axis('off') 
    plt.text(1, 1, str(n), fontsize=14, color = 'y') 
    plt.savefig("test_n = " + str(n) +".png", dpi=300, bbox_inches='tight', pad_inches=0) 

enter image description here

어떻게 그 때마다 삭제합니까?

답변

2

cmap=my_cmap 대신 cmaps[i]을 사용해야합니까? plt.figure()을 사용하여 각 루프에서 숫자를 만들면 제대로 작동합니다.

for i, n in zip(np.arange(4), np.arange(2,6)): 
    plt.figure() 
    plt.imshow(np.power(rndm, n), cmap=cmaps[i], interpolation = 'none') 
    plt.axis('off') 
    plt.text(1, 1, str(n), fontsize=14, color = 'y') 
    plt.savefig("test_n = " + str(n) +".png", dpi=300, bbox_inches='tight', pad_inches=0) 

결과 :

  • 주 화상 (저장되지) enter image description here

-Saved 이미지 :

enter image description here enter image description here enter image description here 라벨 2에서 때문에이 라인의 시작한다는 enter image description here

참고 : 당신이 1부터 시작합니다

plt.text(1, 1, str(n), fontsize=14, color = 'y') 

str(i)str(n)을 변경합니다.

+0

죄송합니다. cmap = my_cmap은 오타였습니다. 내 원래 코드에서 나는 그것이 옳았다. 그러나 plt.figure()는 그것을 고칠 것이다. 답변 주셔서 감사합니다. 나는 그것이 지수이기 때문에 str (n)을 좋아한다. – MyCarta

+0

@MyCarta 알겠습니다, 행운을 빈다! – Yugi

+0

이제 cmap = my_cmap 오류를 제거하는 질문을 편집하여 향후 독자에게 혼동을주지는 않습니다. 다시 한 번 감사드립니다. @ Yugi – MyCarta