【问题标题】:How can I plot a growing data file in real-time with matplotlib?如何使用 matplotlib 实时绘制不断增长的数据文件?
【发布时间】:2017-05-28 11:09:48
【问题描述】:

我正在尝试实时绘制一个文件 (datos.txt),该文件将不断从 pH 传感器获取新数据。

here 所示,我可以绘制一个数据文件,但我仍然无法实时绘制。我正在使用以下代码:

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

我看过一些实时绘图的例子,但我不知道如何让我的工作

【问题讨论】:

标签: python datetime matplotlib plot


【解决方案1】:

您可以将您的情节包装成animate 函数,如下所示:

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

这将每秒重新读取并显示您的情节。

【讨论】:

    猜你喜欢
    • 2012-10-22
    • 2011-05-16
    • 1970-01-01
    • 2022-01-02
    • 2013-07-11
    • 1970-01-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多