轴距不断发生变化,但我想观察x=150,而不经常改变左和右翼,这是不方便的。 我应该做些什么来使图表和红线中断,同时把150个中心放在中间?
The x-axis range keeps changing in this code, but I want to observe the point at x=150 without constantly shifting the point left and right. What should I do to keep the graph and the red line from being cut off while keeping x=150 centered in the middle? Please help. I m using translator, so sorry for my bad english.
import matplotlib.pyplot as plt
import numpy as np
import keyboard
def quadratic(x):
return x ** 2
# 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)
num = 150
count = 0
def updatepoints(num, count, pax, pay, pbx, pby):
if count % 2 == 0:
pax = 150 - 2 * num
pay = quadratic(pax)
else:
pbx = 150 + 2 * num
pby = quadratic(pbx)
return pax, pay, pbx, pby
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- )
pa, = plt.plot(pax, pay, ro , label= point A )
pb, = plt.plot(pbx, pby, ro , label= point B )
# 접선 그리기 (파란색으로)
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.plot(tx,ty, bo )
plt.legend()
while True:
plt.pause(0.1)
if keyboard.is_pressed( a ):
num = num / 5
pax, pay, pbx, pby = updatepoints(num, count, pax, pay, pbx, pby)
plt.pause(0.1)
count = count + 1
print(pax,pbx)
# 그래프 업데이트
x = np.arange(min(pax, pbx), max(pax, pbx), 0.001)
y = quadratic(x)
# 빨간 선 업데이트
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()