English 中文(简体)
我怎么能够把轴心放在150左右?
原标题:How can I keep the x-axis centered around 150?

轴距不断发生变化,但我想观察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()
问题回答

限制不是逐条动态地更新,而是对预期的x数值范围设定限制,这些数值似乎为<代码>[0、300]。

plt.xlim(0,300)




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签