Issue
I created a program in which the shape of the graph gradually expands when the a key is pressed. In order to explain the mathematical concept of limit, I wanted to show the change of the straight line where the two red dots on the graph appear close to the target point. But the red line that came out at the beginning is not disappearing, so I need help. I'm using translator sorry
import matplotlib.pyplot as plt
import numpy as np
import keyboard
def quadratic(x):
return x ** 2
num = 150
def renum(k):
return k / 10
#point a (점 a 초기 설정)
pax=0
pay=quadratic(pax)
#point b (점 b 초기 설정)
pbx=300
pby=quadratic(pbx)
# 초기 그래프 생성 (검은색;이차함수)
x = np.arange(0, 300, 0.001)
y = quadratic(x)
plt.figure(figsize=(8, 6))
line, = plt.plot(x, y, color='black', label='y=x^2')
plt.xlabel('x')
plt.ylabel('y')
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
#점a와b 추가
plt.plot([pax, pbx], [pay, pby], 'r-')
plt.plot(pax,pay,'ro',label=pax)
plt.plot(pbx,pby,'ro',label=pbx)
#접선 그리기 (파란색으로)
tx=150
ty=quadratic(tx)
tslope=2*tx #이차함수의 미분값
tline=tslope*(x-tx)+ty #미분계수를 이용한 접선의 방정식
plt.plot(x,tline, color='blue', label='Tangent at x=150')
plt.legend()
while True:
plt.pause(0.1)
if keyboard.is_pressed('a'):
num = renum(num) # num 값을 수정
plt.pause(0.1)
# 그래프 업데이트
x = np.arange(150 - 2 * num, 150 + 2 * num, 0.001)
y = quadratic(x)
pax=150 - 2 * num
pay=quadratic(pax)
pbx=150 + 2 * num
pby=quadratic(pbx)
plt.plot([pax, pbx], [pay, pby], 'r-')
plt.plot(pax,pay,'ro',label=pax)
plt.plot(pbx,pby,'ro',label=pbx)
plt.xlim(min(x), max(x))
plt.ylim(min(y), max(y))
plt.draw()
Solution
The plt.plot
calls in the while loop are creating new lines. To update the originally plotted line you can get a reference from the initial plt.plot
call and call set_data
methods on that line, here is an example with inline comments:
%matplotlib widget
import matplotlib.pyplot as plt
import numpy as np
import keyboard
def quadratic(x):
return x ** 2
num = 150
def renum(k):
return k / 10
#point a (점 a 초기 설정)
pax=0
pay=quadratic(pax)
#point b (점 b 초기 설정)
pbx=300
pby=quadratic(pbx)
# 초기 그래프 생성 (검은색;이차함수)
x = np.arange(0, 300, 0.001)
y = quadratic(x)
plt.figure(figsize=(8, 6))
line, = plt.plot(x, y, color='black', label='y=x^2')
plt.xlabel('x')
plt.ylabel('y')
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
#점a와b 추가
# get references for plotted red line and points
red_line = plt.plot([pax, pbx], [pay, pby], 'r-')[0]
pa = plt.plot(pax,pay,'ro',label=pax)[0]
pb = plt.plot(pbx,pby,'ro',label=pbx)[0]
#접선 그리기 (파란색으로)
tx=150
ty=quadratic(tx)
tslope=2*tx #이차함수의 미분값
tline=tslope*(x-tx)+ty #미분계수를 이용한 접선의 방정식
plt.plot(x,tline, color='blue', label='Tangent at x=150')
plt.legend()
while True:
plt.pause(0.1)
if keyboard.is_pressed('a'):
num = renum(num) # num 값을 수정
plt.pause(0.1)
# 그래프 업데이트
x = np.arange(150 - 2 * num, 150 + 2 * num, 0.001)
y = quadratic(x)
pax=150 - 2 * num
pay=quadratic(pax)
pbx=150 + 2 * num
pby=quadratic(pbx)
# update original red line and points
red_line.set_data([pax, pbx], [pay, pby])
pa.set_data(pax,pay)
pb.set_data(pbx,pby)
plt.xlim(min(x), max(x))
plt.ylim(min(y), max(y))
plt.draw()
Answered By - Paddy Harrison
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.