I'm trying to create a live plot which updates as more data is available.
import os,sys
import matplotlib.pyplot as plt
import time
import random
def live_plot():
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_xlabel('Time (s)')
ax.set_ylabel('Utilization (%)')
ax.set_ylim([0, 100])
ax.set_xlim(left=0.0)
plt.ion()
plt.show()
start_time = time.time()
traces = [0]
timestamps = [0.0]
# To infinity and beyond
while True:
# Because we want to draw a line, we need to give it at least two points
# so, we pick the last point from the previous lists and append the
# new point to it. This should allow us to create a continuous line.
traces = [traces[-1]] + [random.randint(0, 100)]
timestamps = [timestamps[-1]] + [time.time() - start_time]
ax.set_xlim(right=timestamps[-1])
ax.plot(timestamps, traces, 'b-')
plt.draw()
time.sleep(0.3)
def main(argv):
live_plot()
if __name__ == '__main__':
main(sys.argv)
The above code works. However, I'm unable to interact with the window generated by plt.show()
How can I plot live data while still being able to interact with the plot window?