# Live plotting sensor data

**URL:** <https://discuss.pynq.io/t/live-plotting-sensor-data/6221>\
**Category:** Support\
**Created:** [October 23, 2023, 11:52pm UTC](https://discuss.pynq.io/t/live-plotting-sensor-data/6221 "2023-10-23T23:52:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ntahoturi5922](https://avatars.discourse-cdn.com/v4/letter/n/e47774/32.png) [@ntahoturi5922](https://discuss.pynq.io/u/ntahoturi5922)\
**Post date:** [October 23, 2023, 11:52pm UTC](https://discuss.pynq.io/t/live-plotting-sensor-data/6221/1 "2023-10-23T23:52:47Z")

</div>

Greeting everyone.

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

# Create figure for plotting

fig = plt.figure()  
ax = fig.add\_subplot(1, 1, 1)  
xs =   
ys =

# Initialize the I2C interface

iic = AxiIIC(ol.ip\_dict[“axi\_iic\_0”])  
bindto = [‘[xilinx.com](http://xilinx.com):ip:axi\_iic:2.0’]

# 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)

plt.show()

---

<div class="post-metadata">

**Author:** ![mizan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.pynq.io/mizan/32/2119_2.png) [@mizan](https://discuss.pynq.io/u/mizan)\
**Post date:** [October 24, 2023, 6:04am UTC](https://discuss.pynq.io/t/live-plotting-sensor-data/6221/2 "2023-10-24T06:04:13Z")

</div>

hi @ntahoturi5922,  
it doesn’t seem like a pynq related question, more like python programming though.

> [@ntahoturi5922](#):
>
> ```auto
> 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])
> 
> ```

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

```auto
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)

```

replace y data with your desired value.

Mizan
