在matplotlib python中限制x轴

Posted

技术标签:

【中文标题】在matplotlib python中限制x轴【英文标题】:Limit the x axis in matplotlib python 【发布时间】:2020-07-08 00:58:26 【问题描述】:

我有生成实时图表的代码,每隔几秒更新一次。除了单个问题之外,它的所有功能都完全符合我的要求,x 轴不断添加新值,但从不删除旧值

在下面的示例代码中,因为我将数据框限制为 6 列,所以我希望在我的 x 轴上看到的测量值永远不会超过 6 个。相反,图表会继续更新,最终这些点靠得太近了。

from matplotlib import pyplot
from matplotlib.animation import FuncAnimation
import pandas as pd
from datetime import datetime
import threading
import random
import time
measurements = ['abc','bcd','afr','reg','wow']
counter = 0
figure = pyplot.figure()
measurement_frame = pd.DataFrame(index = measurements)
def get_live(counter2, col_num):
    measurement_frame.iat[counter2,col_num] = random.randint(50,80)
def add_to_dataframe():
    global measurement_frame
    #timey = datetime.now().strftime('%H:%M:%S')
    timey = datetime.now().time()
    if measurement_frame.shape[1] == 6:
        measurement_frame.drop(measurement_frame.columns[0], axis = 1, inplace = True)
    measurement_frame[timey] = measurements
    col_num = measurement_frame.shape[1]-1
    print(col_num)
    counter2 = 0
    for item in measurements:
        t = threading.Thread(target=get_live, args=(counter2, col_num,))
        t.start()
        counter2 = counter2 +1
    t.join()
    print(measurement_frame.columns[0])
    time.sleep(1)
def update(frame):
    add_to_dataframe()
    x_data = measurement_frame.columns
    print(x_data[0])
    y1_data = measurement_frame.loc[measurement_frame.index[0]]
    y2_data = measurement_frame.loc[measurement_frame.index[1]]
    y3_data = measurement_frame.loc[measurement_frame.index[2]]
    y4_data = measurement_frame.loc[measurement_frame.index[3]]
    y5_data = measurement_frame.loc[measurement_frame.index[4]]
    line, = pyplot.plot_date(x_data, y1_data, '-', color = 'b')
    line2, = pyplot.plot_date(x_data, y2_data, '-', color = 'g')
    line3, = pyplot.plot_date(x_data, y3_data, '-', color = 'r')
    line4, = pyplot.plot_date(x_data, y4_data, '-', color = 'm')
    line5, = pyplot.plot_date(x_data, y5_data, '-', color = 'y')
    line.set_data(x_data, y1_data)
    line2.set_data(x_data, y2_data)
    line3.set_data(x_data, y3_data)
    line4.set_data(x_data, y4_data)
    line5.set_data(x_data, y5_data)
    figure.gca().set_xlim(x_data[0])
    figure.gca().autoscale()
    print(figure.gca().get_xlim())
    return line, line2, line3, line4, line5,
animation = FuncAnimation(figure, update, interval=1000)
pyplot.show()

我需要的是在数据框达到最大大小后,删除最左边的测量值,以免一次超过屏幕上设定的测量值。请注意,当数据框达到一定大小时,数据框在添加新列之前已经删除了不需要的列,但我的图表没有反映这一点

【问题讨论】:

set_xlim 方法在这种情况下有帮助吗? 如果我的时间不是一个字符串,我相信它会有所帮助。我不认为它了解如何使用它。你知道它是否可以解释日期时间对象 你能显示pyplot.get_xlim()的输出吗? 我注意到我的输出是你正在寻找的,或者你会在它不运行时理解它,所以我编辑了上面的代码,以便你可以在本地运行它 【参考方案1】:

使用自动缩放会尝试保留旧数据。如果您放弃自动缩放并使用

figure.gca().set_xlim(left =x_data[0], right = datetime.now().time()) 

它按预期工作

现在是完整的代码

from matplotlib import pyplot
from matplotlib.animation import FuncAnimation
import pandas as pd
from datetime import datetime
import threading
import random
import time
measurements = ['abc','bcd','afr','reg','wow']
counter = 0
figure = pyplot.figure()
measurement_frame = pd.DataFrame(index = measurements)
def get_live(counter2, col_num):
    measurement_frame.iat[counter2,col_num] = random.randint(50,80)
def add_to_dataframe():
    global measurement_frame
    #timey = datetime.now().strftime('%H:%M:%S')
    timey = datetime.now().time()
    if measurement_frame.shape[1] == 6:
        measurement_frame.drop(measurement_frame.columns[0], axis = 1, inplace = True)
    measurement_frame[timey] = measurements
    col_num = measurement_frame.shape[1]-1
    print(col_num)
    counter2 = 0
    for item in measurements:
        t = threading.Thread(target=get_live, args=(counter2, col_num,))
        t.start()
        counter2 = counter2 +1
    t.join()
    print(measurement_frame.columns[0])
    time.sleep(1)
def update(frame):
    add_to_dataframe()
    x_data = measurement_frame.columns
    print(x_data[0])
    y1_data = measurement_frame.loc[measurement_frame.index[0]]
    y2_data = measurement_frame.loc[measurement_frame.index[1]]
    y3_data = measurement_frame.loc[measurement_frame.index[2]]
    y4_data = measurement_frame.loc[measurement_frame.index[3]]
    y5_data = measurement_frame.loc[measurement_frame.index[4]]
    line, = pyplot.plot_date(x_data, y1_data, '-', color = 'b')
    line2, = pyplot.plot_date(x_data, y2_data, '-', color = 'g')
    line3, = pyplot.plot_date(x_data, y3_data, '-', color = 'r')
    line4, = pyplot.plot_date(x_data, y4_data, '-', color = 'm')
    line5, = pyplot.plot_date(x_data, y5_data, '-', color = 'y')
    line.set_data(x_data, y1_data)
    line2.set_data(x_data, y2_data)
    line3.set_data(x_data, y3_data)
    line4.set_data(x_data, y4_data)
    line5.set_data(x_data, y5_data)
    figure.gca().set_xlim(left =x_data[0], right = datetime.now().time())
    print(figure.gca().get_xlim())
    return line, line2, line3, line4, line5,
animation = FuncAnimation(figure, update, interval=1000)
pyplot.show()

【讨论】:

以上是关于在matplotlib python中限制x轴的主要内容,如果未能解决你的问题,请参考以下文章

Python,x轴标题与matplotlib中的刻度标签重叠

在python上使用matplotlib挤压x轴图形比例[重复]

在Python matplotlib中更改X轴步骤[重复]

使用 seaborn/matplotlib 创建具有轴限制的图形 [重复]

python使用matplotlib可视化为X轴设置轴标签位置及其对应的标签文本(set label locations and corresponding labels in matplotlib

请教如何自定义python的matplotlib中的X轴刻度的问题