I'm trying to plot, in real-time, a file (datos.txt) that will continuously get new data from a pH sensor.
As shown here, I was able to plot a data file, but I'm still unable to do it in real time. I'm using the following code:
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
import numpy as np
# Converter function
datefunc = lambda x: mdates.date2num(datetime.strptime(x, '%d-%m-%Y %H:%M:%S'))
# Read data from 'file.dat'
dates, levels = np.genfromtxt('/home/ramiro/Programas/pythonProgs/datos.txt', # Data to be read
delimiter=19, # First column is 19 characters wide
converters={0: datefunc}, # Formatting of column 0
dtype=float, # All values are floats
unpack=True) # Unpack to several variables
fig = plt.figure()
ax = fig.add_subplot(111)
# Configure x-ticks
ax.set_xticks(dates) # Tickmark + label at every plotted point
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax.locator_params(axis='x',nbins=10)
ax.plot_date(dates, levels, ls='-', marker='o')
ax.set_title('Hora')
ax.set_ylabel('pH')
ax.grid(True)
# Format the x-axis for dates (label formatting, rotation)
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
plt.show()
I have seen some examples of real-time plotting, but I can't figure out how to make mine work