Development Tip

matplotlib를 사용하여 두 그림을 표시하는 방법은 무엇입니까?

yourdevel 2020. 11. 23. 20:18
반응형

matplotlib를 사용하여 두 그림을 표시하는 방법은 무엇입니까?


하나의 플롯에 표시되지 않고 동시에 두 개의 그림을 그리는 동안 몇 가지 문제가 있습니다. 그러나 문서에 따르면 코드를 작성했으며 그림 1 만 표시됩니다. 중요한 걸 잃어버린 것 같아요. 누구든지 알아낼 수 있습니까? 감사. (코드에 사용 된 * tlist_first *는 데이터 목록입니다.)

plt.figure(1)
plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')
plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')

plt.axvline(x = 30, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 60, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend()
plt.xlim(0,120)
plt.ylim(0,1) 
plt.show()
plt.close() ### not working either with this line or without it

plt.figure(2)
plt.hist(tlist_first, bins=2000000, normed = True, histtype ="step", cumulative = True, color = 'g',label = 'first answer')

plt.ylabel('Percentage of answered questions')
plt.xlabel('Minutes elapsed after questions are posted')

plt.axvline(x = 240, ymin = 0, ymax = 1, color = 'r', linestyle = '--', label = '30 min')
plt.axvline(x = 1440, ymin = 0, ymax = 1, color = 'c', linestyle = '--', label = '1 hour')
plt.legend(loc= 4)
plt.xlim(0,2640)
plt.ylim(0,1)
plt.show()

plt.show()스크립트 끝에서 호출하는 대신 다음을 수행하여 각 Figure를 개별적으로 제어 할 수도 있습니다.

f = plt.figure(1)
plt.hist........
............
f.show()

g = plt.figure(2)
plt.hist(........
................
g.show()

raw_input()

이 경우 raw_input숫자를 살리기 위해 전화해야합니다 . 이렇게하면 표시 할 그림을 동적으로 선택할 수 있습니다.

참고 : Python 3에서 raw_input()로 이름이 변경되었습니다 input().


plt.show()모든 플롯을 만든 후 마지막에 호출해야 합니다.


나는 이와 같은 문제가 있었다.


한 :

f1 = plt.figure(1)

# code for figure 1

# don't write 'plt.show()' here


f2 = plt.figure(2)

# code for figure 2

plt.show()


Write 'plt.show()' only once, after the last figure. Worked for me.


Alternatively, I would suggest turning interactive on in the beginning and at the very last plot, turn it off. All will show up, but they will not disappear as your program will stay around until you close the figures.

import matplotlib.pyplot as plt
from matplotlib import interactive

plt.figure(1)
... code to make figure (1)

interactive(True)
plt.show()

plt.figure(2)
... code to make figure (2)

plt.show()

plt.figure(3)
... code to make figure (3)

interactive(False)
plt.show()

참고URL : https://stackoverflow.com/questions/7744697/how-to-show-two-figures-using-matplotlib

반응형