반응형
Matplotlib (pyplot) savefig는 빈 이미지를 출력합니다.
matplotlib을 사용하여 만든 플롯을 저장하려고합니다. 그러나 이미지가 공백으로 저장됩니다.
내 코드는 다음과 같습니다.
plt.subplot(121)
plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.subplot(122)
y = copy.deepcopy(tumorStack)
y = np.ma.masked_where(y == 0, y)
plt.imshow(dataStack, cmap=mpl.cm.bone)
plt.imshow(y, cmap=mpl.cm.jet_r, interpolation='nearest')
if T0 is not None:
plt.subplot(123)
plt.imshow(T0, cmap=mpl.cm.bone)
#plt.subplot(124)
#Autozoom
#else:
#plt.subplot(124)
#Autozoom
plt.show()
plt.draw()
plt.savefig('tessstttyyy.png', dpi=100)
그리고 tessstttyyy.png는 비어 있습니다 (.jpg로 시도)
첫째, 어떻게됩니까 T0 is not None
? 나는 그것을 테스트 한 다음 전달하는 값을 조정합니다 plt.subplot()
. 값 131, 132 및 133 또는 T0
존재 여부에 따라 다른 값을 사용해보십시오 .
두 번째로, plt.show()
호출 된 후 새 그림이 작성됩니다. 이 문제를 해결하기 위해
전화
plt.savefig('tessstttyyy.png', dpi=100)
하기 전에 전화plt.show()
당신이 이전에 그림을 저장
show()
호출하여plt.gcf()
다음 호출 할 수 있습니다, "현재의 모습을 얻을"에 대한savefig()
이에Figure
언제든지 객체입니다.
예를 들면 다음과 같습니다.
fig1 = plt.gcf()
plt.show()
plt.draw()
fig1.savefig('tessstttyyy.png', dpi=100)
코드에서 'tesssttyyy.png'는 아무것도 그려지지 않은 새 그림을 저장하기 때문에 비어 있습니다.
plt.show()
와야한다 plt.savefig()
나를 위해 문제 를 해결 한 함수의 순서를 변경하십시오 .
- 먼저 줄거리를 저장
- 그런 다음 줄거리 표시
다음과 같이 :
plt.savefig('heatmap.png')
plt.show()
좀 더 자세한 예를 들어 보겠습니다.
import numpy as np
import matplotlib.pyplot as plt
def draw_result(lst_iter, lst_loss, lst_acc, title):
plt.plot(lst_iter, lst_loss, '-b', label='loss')
plt.plot(lst_iter, lst_acc, '-r', label='accuracy')
plt.xlabel("n iteration")
plt.legend(loc='upper left')
plt.title(title)
plt.savefig(title+".png") # should before plt.show method
plt.show()
def test_draw():
lst_iter = range(100)
lst_loss = [0.01 * i + 0.01 * i ** 2 for i in xrange(100)]
# lst_loss = np.random.randn(1, 100).reshape((100, ))
lst_acc = [0.01 * i - 0.01 * i ** 2 for i in xrange(100)]
# lst_acc = np.random.randn(1, 100).reshape((100, ))
draw_result(lst_iter, lst_loss, lst_acc, "sgd_method")
if __name__ == '__main__':
test_draw()
참고 URL : https://stackoverflow.com/questions/9012487/matplotlib-pyplot-savefig-outputs-blank-image
반응형
'IT story' 카테고리의 다른 글
소프트 키보드가 표시 될 때 레이아웃을 위로 이동 하시겠습니까? (0) | 2020.07.06 |
---|---|
루비 클래스 유형 및 사례 설명 (0) | 2020.07.06 |
Android의 새로운 하단 탐색 모음 또는 BottomNavigationView (0) | 2020.07.06 |
키워드 "use"는 PHP에서 어떻게 작동하며 클래스를 가져올 수 있습니까? (0) | 2020.07.06 |
NSInteger에 대한 NSLog / printf 지정자? (0) | 2020.07.06 |