0

This code I get from a GitHub Link here.

I used the data_gen code to generate random numbers and continually write these numbers into the CSV file. Then, I used the code inside snippets.txt to do real-time plotting.

I am using the Colab environment to run the two codes and both in the same directory. The issue I am facing is that the code is not shown the plot in real-time, instead, it plots the data that is being saved in the CSV file after I interrupt the data_gen code.

Here is the two code:

Frist Code:

# data_gen code
    import csv
    import random
    import time
    import matplotlib.pyplot as plt
    
    x_value = 0
    total_1 = 1000
    total_2 = 1000
    
    fieldnames = ["x_value", "total_1", "total_2"]
    
    with open('/content/gdrive/MyDrive/data_ranadom.csv', 'w') as csv_file:
        csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        csv_writer.writeheader()
    
    while True:
        with open('/content/gdrive/MyDrive/data_ranadom.csv', 'a') as csv_file:
            csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    
            info = {
                "x_value": x_value,
                "total_1": total_1,
                "total_2": total_2
            }
    
            csv_writer.writerow(info)
            print(x_value, total_1, total_2)
    
            x_value += 1
            total_1 = total_1 + random.randint(-6, 8)
            total_2 = total_2 + random.randint(-5, 6)
            time.sleep(1)

Second Code:

# Data_plot code
import os
import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

plt.ion()
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('/content/gdrive/MyDrive/data_ranadom.csv')
    x = data['x_value']
    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)

    xlim_low, xlim_high = ax.get_xlim()
    ylim_low, ylim_high = ax.get_ylim()

    ax.set_xlim(xlim_low, (x.max() + 5))

    y1max = y1.max()
    y2max = y2.max()
    current_ymax = y1max if (y1max > y2max) else y2max

    y1min = y1.min()
    y2min = y2.min()
    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()

The data_gen code seems to be working as expected i.e., generating random numbers and storing them in a CVS file.

The problem I guess is in the data_plot code since it seems it's not monitoring the CSV file in real-time. Instead, it plots the saved data in the CSV file. Although, the code is working fine in the coder youtube channel (Tutorial Link here).

Would someone help me find a way to modify the code to make it work?

I tried to use %%writefile test2.py for the data_plot code and then call this python script in the data_gen code using !python '/content/gdrive/MyDrive/test2.py' but it did not show any plot.

user34755
  • 1
  • 2

1 Answers1

0

For the second script, you have to create a loop to plot every two or three seconds, also you have to use plt.pause() to give time Matplotlib to plot.

import os
import random
from itertools import count
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

plt.ion()
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_random.csv') 
    x = data['x_value']
    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)

    xlim_low, xlim_high = ax.get_xlim()
    ylim_low, ylim_high = ax.get_ylim()

    ax.set_xlim(xlim_low, (x.max() + 5))

    y1max = y1.max()
    y2max = y2.max()
    current_ymax = y1max if (y1max > y2max) else y2max

    y1min = y1.min()
    y2min = y2.min()
    current_ymin = y1min if (y1min < y2min) else y2min

    ax.set_ylim((current_ymin - 5), (current_ymax + 5))


while True:
    
    ani = FuncAnimation(plt.gcf(), animate, interval=1000)
    
    plt.legend()
    plt.tight_layout()
    plt.pause(3) # Number of seconds you wait to update the plot

By the way, it is weird that when I close the window, it cannot show the plot again. Maybe you can fix that.

David
  • 435
  • 3
  • 12
  • Thanks for your answewr. It didn't work on colab environment. I guss the easy way is to run both scripts locally and it should work. – user34755 Jun 07 '21 at 16:13