Matplotlib에서 텍스트를 새로 고치려면 다음 단계를 수행할 수 있습니다. -
- 그림 크기를 설정하고 서브플롯 사이 및 주변 여백을 조정합니다.
- 그림과 서브플롯 세트를 생성합니다.
- 축에 텍스트를 추가합니다.
- "z" 및 "c" 키를 기반으로 텍스트를 업데이트하는 사용자 지정 메서드를 작성합니다.
- key_press_event로 함수 작업 바인딩 .
- 그림이 포함된 캔버스를 그립니다.
- 텍스트로 그림에 애니메이션 효과를 줍니다.
- 그림을 표시하려면 show()를 사용하세요. 방법.
예시
from matplotlib import pyplot as plt, animation
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
fig, ax = plt.subplots()
text = ax.text(.5, .5, 'First Text')
def action(event):
if event.key == "z":
text.set_text("zoom")
elif event.key == "c":
text.set_text("cool")
fig.canvas.mpl_connect('key_press_event', action)
fig.canvas.draw()
animation = animation.ArtistAnimation(fig, [(text,)])
plt.show() 출력
