1

update codeI'm plotting real-time data using parsed data from a file that is being repeatedly opened. The plotting works great until the X axis (time) runs out a room. I'm trying to figure out a way to advance to the next element and shift the time values to the left. Code and screenshot included here.

import matplotlib.pyplot as plt
import csv
import datetime
from matplotlib.animation import FuncAnimation

x = []
y = []
rssi_val = []

def animate(i):
    with open('stats.txt', 'r') as searchfile:
#        time = (searchfile.read(5))
        time = (searchfile.read(8))
        for line in searchfile:
            if 'agrCtlRSSI:' in line:
                rssi_val = line[16:20]
                y.append(rssi_val)
                x.append(time[-1])


    plt.cla()
    plt.plot(x,y)
    next(x)
    plt.xlabel('Time')
    plt.ylabel('RSSI')
    plt.title('Real time signal strength seen by client X')
    plt.tight_layout()

ani = FuncAnimation(plt.gcf(), animate, interval=5000)
plt.show()

enter image description here

1 Answer 1

1

You could just rotate the labels,

for label in ax.get_xticklabels():
    label.set_rotation(90)

or

ax.tick_params('x', labelrotation=90)

before calling plt.plot().

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks again William. However, when I add that just above plt.plot(x,y) I get "AttributeError: 'list' object has no attribute 'get_xticklabels'. I'm thinking it's because I didn't define something correctly (?).
Preface it with ax=plt.gca() or use plt.gca().get_xticklabels()
I tried the first suggestion, followed by the second and unfortunately the time values are still getting squeezed. Entire snippet in updated image above.
@wiwinut Needs to be after plt.plot()
You have come to my rescue once again. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.