如何让动画图在x轴上显示时间而不是数值

时间:2017-07-18 16:00:28

标签: python animation matplotlib

我正在从csv文件中读取数据并使用matplotlib动画绘制“实时流”。一切都运行正常,除了我想在x轴上显示时间而不是“matplotlib.dates.date2num”值。有一种简单的方法可以做到这一点吗?

import numpy 
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import animation
import datetime
from numpy import genfromtxt


cv = numpy.genfromtxt ('file.csv', delimiter=",")
second = cv[:,0]
third = cv[:,2]
FMT = '%Y-%m-%d-%H%M%S.%f'
data = numpy.genfromtxt('file.csv', delimiter=',', skip_header=2,names=['t', 'in', 'x', 'y','z'], dtype=['object', 'int8', 'float', 'float', 'float'])                  
d = [datetime.datetime.strptime(i.decode('ascii'), FMT) for i in data['t']]

conversion3 = [matplotlib.dates.date2num(j) for j in d]
mytime3 = numpy.array(conversion3)

x = mytime3
y = third               
fig, ax = plt.subplots()
line, = ax.plot([], [], 'b-')
ax.margins(0.05)

def init():
    line.set_data(x[:2],y[:2])
    return line,

def animate(i):
    win = 150
    imin = min(max(0, i - win), x.size - win)
    xdata = x[imin:i]
    ydata = y[imin:i]
    line.set_data(xdata, ydata)

    ax.relim()
    ax.autoscale()
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=25)

plt.show()

csv文件的格式为:

2016-07-11-095303.810,1,79
2016-07-11-095303.900,1,77
2016-07-11-095303.990,1,59
2016-07-11-095304.080,1,48
2016-07-11-095304.170,1,48
2016-07-11-095304.260,1,77
2016-07-11-095304.350,1,81
2016-07-11-095304.440,1,63
2016-07-11-095304.530,1,54
2016-07-11-095304.620,1,29

1 个答案:

答案 0 :(得分:0)

您可以使用plot_date代替plot,这会自动格式化刻度。然后你应该绘制日期,而不是转换后的数字。

以下运行良好:

u = u"""2016-07-11-095303.810,1,79
2016-07-11-095303.900,1,77
2016-07-11-095303.990,1,59
2016-07-11-095304.080,1,48
2016-07-11-095304.170,1,48
2016-07-11-095304.260,1,77
2016-07-11-095304.350,1,81
2016-07-11-095304.440,1,63
2016-07-11-095304.530,1,54
2016-07-11-095304.620,1,29"""

import io
import numpy 
import matplotlib.pyplot as plt
from matplotlib import animation
import datetime
from numpy import genfromtxt


cv = numpy.genfromtxt (io.StringIO(u), delimiter=",")
second = cv[:,0]
third = cv[:,2]
FMT = '%Y-%m-%d-%H%M%S.%f'
data = numpy.genfromtxt(io.StringIO(u), delimiter=',', skip_header=2,
                        names=['t', 'in', 'x', 'y','z'], 
                        dtype=['object', 'int8', 'float'])
d = [datetime.datetime.strptime(i.decode('ascii'), FMT) for i in data['t']]


x = d
y = data["x"]        
fig, ax = plt.subplots()
line, = ax.plot_date([], [], 'b-')
ax.margins(0.05)

def init():
    line.set_data(x[:2],y[:2])
    return line,

def animate(i):
    imin = 0 #min(max(0, i - win), x.size - win)
    xdata = x[imin:i+2]
    ydata = y[imin:i+2]
    line.set_data(xdata, ydata)

    ax.relim()
    ax.autoscale()
    return line,

anim = animation.FuncAnimation(fig, animate, frames=7,init_func=init, interval=150)

plt.show()