1

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

1

1 Answer 1

1

You could wrap your plot into an animate function as follows:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.animation as animation
from datetime import datetime
import numpy as np    

def animate(i, fig, ax):
    # 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

    # 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, 'k', 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()

fig = plt.figure()
ax = fig.add_subplot(111)
ani = animation.FuncAnimation(fig, animate, fargs=(fig, ax), interval=1000)
plt.show() 

This would reread and display your plot every second.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.