AlbinoRhino
AlbinoRhino

Reputation: 497

Limit the x axis in matplotlib python

I have code that produces a live graph, updating every few seconds. It all functions EXACTLY as I want other than a single issue, the x axis keeps adding new values but never removing old ones

in the example code below, because I limit the dataframe to 6 columns, I expect to never see more than 6 measurements represented on my x-axis. Instead, the graph continues to update and eventually the poiunts are too close together.

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

What I need is that after the dataframe reaches maximum size, the far left measurements are removed, so as to not exceed a set number of measurements on the screen at once. Note that the dataframe already drops unneeded columns before adding a new one when it reaches a certain size, but my graph does not reflect that

Upvotes: 2

Views: 85

Answers (1)

AlbinoRhino
AlbinoRhino

Reputation: 497

using autoscale tries to keep old data in view. If you drop autoscale and use

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

it works as intended

the full code is now

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

Upvotes: 2

Related Questions