I am trying to plot data comming from an i2c sensor device. I can read data just fine but I am having truble plotting the data. bellow is the code I have tried so far. any help is appriciated
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from pynq.lib import AxiIIC
import numpy as np
This function is called periodically from FuncAnimation
def animate(i, xs, ys):
# Read temperature data
temp_rx_data = bytes(2)
iic.send(0x40, temp_rx_data, 1)
time.sleep(0.5)
for i in range(100):
sensor_data = iic.receive(0x40, temp_rx_data, 2)
data_buffer[i] = mmio.write(0,sensor_data)
temp_c =mmio.read(data_buffer[i])
# Append data to lists
xs.append(dt.datetime.now().strftime('%H:%M:%S.%f'))
ys.append(temp_c)
# Limit x and y lists to a certain number of data points
max_data_points = 100 # You can adjust this to your desired number of data points
xs = xs[-max_data_points:]
ys = ys[-max_data_points:]
# Update the plot with new data
ax.clear()
ax.plot(xs, ys)
# Format plot
plt.xticks(rotation=45, ha='right')
plt.subplots_adjust(bottom=0.30)
plt.title('TMP102 Temperature over Time')
plt.ylabel('Temperature (deg C)')
Set up plot to call animate() function periodically
ani = animation.FuncAnimation(fig, animate, fargs=(xs, ys), interval=1,cache_frame_data=False)
hi @ntahoturi5922,
it doesn’t seem like a pynq related question, more like python programming though.
I hope you are understanding what you are doing here. You are sending two blank bytes and for 100 times you are receiving data, which are not used anywhere. Then you have wrote the respond of iic function (not the iic data) in the buffer, then again respond of writing function which is either 0 or 1 is read by read operation. Also temp_c is overwritten again and again, so at last you will get only one value either 0 or 1.
If this is what you want, that’s okay (But I think you want to have the data coming from the sensor instead). And you already have stated that you can read data fine, so i assume it is somehow working fine as you desired.
for graph the chart you can use clear output instead of FuncAnimation function.
sample code would be
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import display, clear_output
import time
import datetime
import random
%matplotlib inline
# Initialize the figure and axis
fig, ax = plt.subplots()
# Initialize empty lists to store the data
x_data = []
y_data = []
# Set the number of data points to be displayed
max_data_points = 20
# Update the plot in a loop
for i in range(100): # You can change the number of iterations as per your requirement
x_data.append(datetime.datetime.now().strftime('%H:%M:%S.%f'))
y_data.append(random.random())
if len(y_data) > max_data_points:
# Delete the first element in the list to maintain 20 data points
x_data.pop(0)
y_data.pop(0)
ax.clear()
ax.plot(x_data, y_data, marker='o', linestyle='-', color='b')
ax.set_title(f"TMP102 Temperature over Time")
ax.set_xlabel('Time')
ax.set_ylabel('Temperature (deg C)')
display(fig)
clear_output(wait=True)
time.sleep(0.5)