From b66bd4720a68030d30451feb24fa4563b08c1928 Mon Sep 17 00:00:00 2001 From: Corey Schafer Date: Tue, 18 Jun 2019 09:46:20 -0600 Subject: [PATCH] Fixed snippet --- Python/Matplotlib/09-LiveData/snippets.txt | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/Python/Matplotlib/09-LiveData/snippets.txt b/Python/Matplotlib/09-LiveData/snippets.txt index 77f44eb..5d95fff 100644 --- a/Python/Matplotlib/09-LiveData/snippets.txt +++ b/Python/Matplotlib/09-LiveData/snippets.txt @@ -1,4 +1,17 @@ # Another way to do it without clearing the Axis +from itertools import count +import pandas as pd +import matplotlib.pyplot as plt +from matplotlib.animation import FuncAnimation + +plt.style.use('fivethirtyeight') + +x_vals = [] +y_vals = [] + +plt.plot([], [], label='Channel 1') +plt.plot([], [], label='Channel 2') + def animate(i): data = pd.read_csv('data.csv') @@ -6,10 +19,12 @@ def animate(i): y1 = data['total_1'] y2 = data['total_2'] + ax = plt.gca() + line1, line2 = ax.lines + line1.set_data(x, y1) line2.set_data(x, y2) - ax = plt.gca() xlim_low, xlim_high = ax.get_xlim() ylim_low, ylim_high = ax.get_ylim() @@ -24,3 +39,10 @@ def animate(i): current_ymin = y1min if (y1min < y2min) else y2min ax.set_ylim((current_ymin - 5), (current_ymax + 5)) + + +ani = FuncAnimation(plt.gcf(), animate, interval=1000) + +plt.legend() +plt.tight_layout() +plt.show()