【问题标题】:How to make matplotlib update my axes based on range of data如何让 matplotlib 根据数据范围更新我的坐标轴
【发布时间】:2022-01-01 09:14:21
【问题描述】:

我有一个实时动画,我想在每次重绘期间更新 x 和 y 轴。

我尝试了多种方法来解决这个问题,这些方法在下面的代码中保留为 cmets

我现在认为问题出在返回 line 上,这与变量 ax 相关,而 FuncAnimation 作用于变量 fig?

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()

【问题讨论】:

    标签: python matplotlib animation plot data-visualization


    【解决方案1】:

    我能够使用下面的代码让坐标轴动态变化。

    主要区别在于我使用了plt.ylimplt.xlim,而不是更改图形xlim 或ylim。但是,它的 Ive 还在这些代码旁边添加了注释代码,这些代码也可以工作。我相信 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      • 1970-01-01
      • 2018-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多