Animated scatter plot using live data while a line sweep 360

  live, matplotlib, matplotlib-animation, plot, python

I’m trying to create a graph where a beam sweeps 360 degrees around a circle while points are plotted from a live CSV file. The CSV file will be cleared of its data periodically and as such points plotted should disappear once the data is no longer in the file. I’ve gotten the beam graphic to sweep around the circle quite nicely, but I’ve been unable to get the scatter points up. Currently it will read and plot the points that are already in the CSV file, but will not plot new ones. Anyone have some suggestions? I’m not especially familiar with the animation package in matplotlib. I’ve included the plotting code and the file I’m using to generate data.

plotting:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation
from random import random
import pandas as pd

r = 90  * (np.pi/180)
t = 6000
point1 = 1500
pointangle = 245 * (np.pi/180)



# Setup figure and background
fig = plt.figure()
plt.rcParams.update({
    "lines.color": "green",
    "patch.edgecolor": "green",
    "text.color": "white",
    "axes.facecolor": "black",
    "axes.edgecolor": "black",
    "axes.labelcolor": "red",
    "xtick.color": "green",
    "ytick.color": "red",
    "grid.color": "green",
    "figure.facecolor": "black",
    "figure.edgecolor": "black",
    "savefig.facecolor": "black",
    "savefig.edgecolor": "black"})
fig.patch.set_facecolor("black")
ax = fig.gca(projection = 'polar')
fig.canvas.set_window_title('ICU PPI')
ax.plot(r, t, color ='black', marker = 'o', markersize = '1')
#ax.plot( pointangle,point1, color ='red', marker = 'o', markersize = '10', alpha = .5)
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_ylim(0,1.02*t)
fig = matplotlib.pyplot.gcf()
fig.set_size_inches(5, 5)
fig.savefig('test2png.png', dpi=100)
fig.set_size_inches(5, 5, forward=True)


#lines to update

line1, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 2, alpha = .5)
line2, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line3, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line4, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line5, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line6, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line7, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line8, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line9, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line10, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line11, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line12, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line13, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line14, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 1, alpha = .5)
line15, = ax.plot([0, 0],[0,t], color = 'green', linewidth = 2, alpha = .5)


#function that updates lines
def update(angle):

    line1.set_data([angle, angle],[0,t])
    line2.set_data([(angle), (angle-.01)], [0, t])
    line3.set_data([(angle), (angle - .02)], [0, t])
    line4.set_data([(angle), (angle - .03)], [0, t])
    line5.set_data([(angle), (angle - .04)], [0, t])
    line6.set_data([(angle), (angle - .05)], [0, t])
    line7.set_data([(angle), (angle - .06)], [0, t])
    line8.set_data([(angle), (angle - .07)], [0, t])
    line9.set_data([(angle), (angle - .08)], [0, t])
    line10.set_data([(angle), (angle - .09)], [0, t])
    line11.set_data([(angle), (angle - .1)], [0, t])
    line12.set_data([(angle), (angle - .11)], [0, t])
    line13.set_data([(angle), (angle - .12)], [0, t])
    line14.set_data([(angle), (angle - .13)], [0, t])
    line15.set_data([(angle), (angle - .14)], [0, t])


    return line1, line2, line3, line4, line5,line6, line7, line8, line9, line10, line11, line12, line13, line14, line15,


#points the update sweeps through
frames = np.linspace(0,2*np.pi,360)




#read data and plot scatter points

def animate(i):
    data = pd.read_csv('data.csv')
    x = data['x_value']
    y = data['total_1']
    point1 = np.sqrt(x**2 + y**2)
    pointangle = np.tan(y/x)

    #ax = fig.gca(projection='polar')
    ax.scatter(r, t,  color='black', marker='o')
    ax.scatter( pointangle,point1, color = "red")# color ='red', marker = 'o', markersize = '10', alpha = .5)


#draw figure

fig.canvas.draw()
ani2 = matplotlib.animation.FuncAnimation(fig, animate,frames = frames, blit = True, interval=1)
ani = matplotlib.animation.FuncAnimation(fig, update, frames=frames, blit = True, interval=1)
plt.show()

data generation:

import csv
import random
import time

x_value = 400
total_1 = 400
total_2 = 1000

fieldnames = ["x_value", "total_1", "total_2"]


with open('data.csv', 'w') as csv_file:
    csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    csv_writer.writeheader()

i = 0
while i < 10:

    with open('data.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 += random.randint(-100, 100)
        total_1 = total_1 + random.randint(-200, 200)
        total_2 = total_2 + random.randint(-5, 6)
    time.sleep(3)
    i += 1

Source: Python Questions

LEAVE A COMMENT