Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

pandas - Python - Animated annotations disapears with liveplot

this thread enter link description here helps me a lot with my problem but now i'm struggling with another thing. I got two scripts, one imitate some real power supply which generates data, simple sin and cos samples, and the second one witch real-time plotting it.

The problem I have is after 60 samples the annotations near the last samples just disapears. Any ideas why is that?

Data generator code:

from pathlib import Path
import csv
import random
import time
from math import sin, cos

x_value = 0
total_1 = 0
total_2 = 0

fieldnames = ["sample_numb", "voltage", "current"]

path = "some_path/data.csv"
file_path = Path(path)
with open(file_path, 'w') as csv_file:
    csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    csv_writer.writeheader()

t = 0
while True:

    with open(file_path, 'a') as csv_file:
        csv_writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

        info = {
            "sample_numb": x_value,
            "voltage": total_1,
            "current": total_2
        }

        csv_writer.writerow(info)

        x_value += 0.2
        total_1 = sin(x_value)
        total_2 = cos(x_value)


    time.sleep(0.1)

Ploting code:

import time
import psutil
import matplotlib.pyplot as plt
import pandas as pd
from pathlib import Path
from matplotlib.animation import FuncAnimation

path = "some_path/data.csv"
file_path = Path(path)

plt.rcParams['animation.html'] = 'jshtml'
fig, (ax1, ax2) = plt.subplots(2,1)
ax1.set_title("Power supply parameters")
ax1.set(ylabel='Voltage [V]')
ax2.set(xlabel='sample [n]', ylabel='Current [A]')
ax1.set_ylim(-1.5, 1.5)
ax2.set_ylim(-1.5,1.5)
ax1.grid(1)
ax2.grid(1)

annotation1 = ax1.annotate(str(0),(10,0.5))
annotation2 = ax2.annotate(str(0),(10,0.5))

def annotate(last_y1, last_y2,xlim):
    annotation1.set_text(str(round(last_y1,2)))
    annotation2.set_text(str(round(last_y2,2)))
    annotation1.set_position((xlim,last_y1))
    annotation2.set_position((xlim,last_y2))
    print(xlim, last_y1)

def data_gen():
    data = pd.read_csv(file_path)
    x = data['sample_numb']
    y1 = data['voltage']
    y2 = data['current']
    
    x = x[-20:]
    y1 = y1[-20:]
    y2 = y2[-20:]
    return x,y1,y2

def run(i):
    x, y1, y2 = data_gen()
    xlim = x.tolist()[-2]
    last_y1 = y1.tolist()[-2]
    last_y2 = y2.tolist()[-2]

    ax1.plot(x, y1, color='b')
    ax2.plot(x, y2, color='r')  

    annotate(last_y1, last_y2,xlim)

    ax1.set_xlim(left=max(0, xlim - 50), right=xlim + 10)
    ax2.set_xlim(left=max(0, xlim - 50), right=xlim + 10)
    

ani = FuncAnimation(fig, run, interval=100)
plt.show()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...