Comments (1)
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)
- [Bug]: for Python 3.11.9 gor error ValueError: PyCapsule_New called with null pointer
- [Bug]: y_label in wrong place after clearing for twinx axes HOT 2
- [ENH]: Ability to add custom colours and colourmaps to matplotlibrc HOT 1
- [Bug]: plt.imshow displays a non-zero array as all zeros HOT 2
- Maximum Alpha for Scatter HOT 7
- [Doc]: Update License Text to standard SPDX license HOT 11
- problem with pip install under Python 3.13.0b1 on Win 11 [Bug]: HOT 10
- [ENH]: Natural 3D rotation with mouse HOT 1
- [Doc]: Missing comma HOT 1
- [Bug]: set linestyle='dashed' raise error with quiver and legend HOT 4
- [ENH] Support for Multi-Color Line Legends in Plots
- [Doc]: Inheritance Diagrams Not Respecting Dark Mode HOT 1
- [Bug]: orientation indication shows up late in mplot3d, and then lingers HOT 1
- [ENH]: Add click to enlarge subplots in a figure HOT 2
- [Bug]: setting patchA of Annotation.arrow_patch has no effect HOT 2
- [Bug]: matplotlib broke compatibility with get_cmap in new release (3.9) HOT 2
- [Bug]: Button causes window to hang HOT 7
- [ENH]: Qt Designer support. HOT 2
- Plot_surface add wrong value HOT 1
- [Bug]: show(block=False) freezes HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from matplotlib.