Giter Club home page Giter Club logo

Comments (1)

timhoffm avatar timhoffm commented on June 22, 2024

Please boil down your problem to a minimal working example. We cannot test your code with a serial device.

When stripping down your code, it works:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import csv


# Initialize empty lists to store data
x_vals = []
sensorValue1_data = []

# Create a function to read and process data from Arduino
def read_and_process_data():
    xn = len(x_vals)+1
    x_vals.append(xn)
    sensorValue1_data.append(np.sin(xn))
    print("read done")


# Create a function to update the plot
def update_plot(frame):
    read_and_process_data()
    plt.cla()
    plt.plot(x_vals, sensorValue1_data, label='Sensor 1')
    plt.xlabel('Time')
    plt.ylabel('distance')
    plt.legend()

# Create a function to save data to a CSV file when the plot window is closed
def on_close(event):
    print("write, write, write")

# Register the callback function for when the plot window is closed
fig, ax = plt.subplots()
fig.canvas.mpl_connect('close_event', on_close)

ani = FuncAnimation(fig, update_plot, interval=1)
plt.show()

General hint: Note that it's faster to not clear and redraw everyting in the update function. Instead, only set_data() add additional data points and update the limits if needed. See e.g. https://matplotlib.org/devdocs/gallery/animation/animate_decay.html

from matplotlib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.