을 만드는 방법 matplotlib 업데이트 축에 기반한 범위의 데이터

0

질문

나는 라이브 애니메이션이 나를 업데이트하고 싶은 x 축과 y 축 각각 다시 그리기.

글로벌 시장을 해결하기 위해 여러 가지 방법으로 이는 왼쪽으로 주석에서 아래 코드

나는 지금 하는 문제가 발생으로 돌아에서 라인,관련되는 변수 ax,동 FuncAnimation 행위 변수에 도?

import pandas as pd
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
global df
df = pd.DataFrame(columns = ['time', 'number'])
global start_time
start_time = time.time()
df['time'] = [1]*40
df['number'] = [1]*40
global counter
counter = 0
while counter<40:
    df.iat[counter, 0] = round(((time.time()-start_time)*10))
    df.iat[counter, 1] = counter
    time.sleep(0.1)
    counter = counter+1
def get_data():
    global counter
    global start_time
    global df
    df.drop(range(10), axis = 0, inplace=True)
    df2 = pd.DataFrame(columns = ['time', 'number'])
    list1 = []
    list2 = []
    for item in range(10):
        time.sleep(0.1)
        list1.append(round(((time.time()-start_time)*10)))
        list2.append(counter)
        counter = counter + 1
    df2['time'] = list1
    df2['number'] = list2
    df = df.append(df2, ignore_index = True)
    df.reset_index(inplace=True, drop = True)
    x_data = df['time']
    y_data = df['number']
    return x_data,y_data
def get_limits():
    global df
    x_min = min(df['time'])
    y_min = min(df['number'])
    x_max = max(df['time'])
    y_max = max(df['number'])
    return x_min, y_min, x_max, y_max
fig, ax = plt.subplots()
def animate(i):
    x_data, y_data= get_data()
    x_min, y_min, x_max, y_max = get_limits()
    #plt.xlim(x_min, x_max, auto = True)
    #plt.ylim(y_min, y_max, auto = True)
    ax.set_xlim(x_min, x_max, auto = True)
    ax.set_ylim(y_min, y_max, auto = True)
    line = ax.plot(x_data, y_data)
    #line = ax.plot(x_data, y_data,scalex=True, scaley=True, color="red")

    #plt.plot(x,y, scaley=True, scalex=True, color="red")
    return line
ani = animation.FuncAnimation(
    fig, animate, interval=50, blit=True, save_count=50)
#ani2 = animation.FuncAnimation(ax, animate, interval = 50, blit=True, save_count=50)
plt.show()
1

최고의 응답

0

을 얻을 수 있었 축을 동적으로 변경으로 아래 코드.

주요 차이점은 사용 plt.ylimplt.xlim반대로 변하는 그림 xlim 또는 ylim. 그러나,그것의 필자 또한 추가 주석 코드는 다음을 사람들,또한 작품이다. 믿 ax1 을입니다 줄거리는 축에 할당된 그림. 따라서,업데이트 ax1 을 업데이트 축을 기준으로 합니다. 이할 수도 있습 액세스 fig.gca()이후 figure.gca() 반환합니다 축의 그림입니다.

import pandas as pd
import time
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random
global df
df = pd.DataFrame(columns = ['time', 'number'])
global start_time
start_time = time.time()
df['time'] = [1]*40
df['number'] = [1]*40
global counter
counter = 0
while counter<40:
    df.iat[counter, 0] = round(((time.time()-start_time)*20))
    df.iat[counter, 1] = counter
    time.sleep(0.05)
    counter = counter+1
def get_data():
    global counter
    global start_time
    global df
    df.drop(range(10), axis = 0, inplace=True)
    df2 = pd.DataFrame(columns = ['time', 'number'])
    list1 = []
    list2 = []
    for item in range(10):
        time.sleep(random.randint(10,100)/1000)
        list1.append(round(((time.time()-start_time)*20)))
        list2.append(counter)
        counter = counter + 1
    df2['time'] = list1
    df2['number'] = list2
    df = df.append(df2, ignore_index = True)
    df.reset_index(inplace=True, drop = True)
    x_data = df['time']
    y_data = df['number']
    return x_data,y_data
def get_limits():
    global df
    x_min = min(df['time'])
    y_min = min(df['number'])
    x_max = max(df['time'])
    y_max = max(df['number'])
    return x_min, y_min, x_max, y_max

fig = plt.figure(figsize = (18,9))
ax1 = fig.add_subplot(1,1,1)
plt.title("Dynamic Axes")

def animate(i):
    x_data, y_data = get_data()
    x_min, y_min, x_max, y_max = get_limits()
    plt.xlim(x_min, x_max) #ax1.set_ylim(y_min, y_max)
    plt.ylim(y_min,y_max) #fig.gca().set_xlim(x_min,x_max)
    plt.plot(x_data,y_data)
animation = animation.FuncAnimation(fig, animate, interval = 50)
plt.show()
2021-11-22 21:33:56

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................