如何将时间范围绘制为Pandas或MatPlotLib中的值

时间:2015-02-20 11:47:00

标签: python-2.7 matplotlib pandas

我有几个包含时间序列数据的DataFrame,并希望创建每个DataFrame的时间范围范围的简单可视化。 由于我无法使用代码生成此代码,因此我添加了草图来说明我的目标。

Time Range Illustration

以下是一些代码,用于创建三个基本上简化的DataFrame,我正在使用的DataFrame的较小版本。

from pandas import DataFrame
from numpy import datetime64, random

# example data recorded by two different sensors
example_data = random.rand(5,2)
example_data2 = random.rand(9,2)
example_data3 = random.rand(9,2)

# timestamps from sensor1
times = ['2000-01-01 09:00:00',
        '2000-01-01 09:15:00',
        '2000-01-01 09:30:00',
        '2000-01-01 09:45:00',
        '2000-01-01 10:00:00']

# timestamps from sensor2
times2 = ['2000-01-01 08:45:00',
        '2000-01-01 09:00:00',
        '2000-01-01 09:15:00',
        '2000-01-01 09:30:00',
        '2000-01-01 09:45:00',
        '2000-01-01 10:00:00',
        '2000-01-01 10:15:00',
        '2000-01-01 10:30:00',
        '2000-01-01 10:45:00']

# timestamps from sensor2
times3 = ['2000-01-01 09:20:00',
        '2000-01-01 09:40:00',
        '2000-01-01 10:00:00',
        '2000-01-01 10:20:00',
        '2000-01-01 10:40:00',
        '2000-01-01 11:00:00',
        '2000-01-01 11:20:00',
        '2000-01-01 11:40:00',
        '2000-01-01 12:00:00']


# create the DataFrame object for sensor1 with the times and data above
sensor1 = DataFrame({'Time': times,
                    'measure1': example_data[:,0],
                    'measure2': example_data[:,1]})

# create the DataFrame object for sensor2 with the times and data above
sensor2 = DataFrame({'Time': times2,
                    'measure1': example_data2[:,0],
                    'measure2': example_data2[:,1]})

# create the DataFrame object for sensor2 with the times and data above
sensor3 = DataFrame({'Time': times3,
                    'measure1': example_data3[:,0],
                    'measure2': example_data3[:,1]})

# coerce the 'Time' column from string to a numpy datetime64 value
sensor1['Time'] = sensor1['Time'].astype(datetime64)
sensor2['Time'] = sensor2['Time'].astype(datetime64)
sensor3['Time'] = sensor3['Time'].astype(datetime64)

我尝试从每个DataFrame中获取最小和最大日期时间值并将它们放入一个新的DataFrame中但是当我尝试绘制它们时,我得到一个错误,即没有要绘制的值。

我还尝试仅使用'Time'列,并将Integer分配给'value'列(即传感器1获取Int 1广播到'value'列,sensor2得到Int 2广播等等on),然后合并这些DataFrame。

但是这会在“时间”列中产生大量重复值,并在“值”列中产生纳米值。

我已经完成了如何让它发挥作用的想法。

编辑:修正了代码块中偷偷摸摸的'2001'时间戳; - )

1 个答案:

答案 0 :(得分:4)

import numpy
import pandas

# create an index containing all time stamps
idx1 = pandas.Index(sensor1.Time)
idx2 = pandas.Index(sensor2.Time)
idx3 = pandas.Index(sensor3.Time)
df = pandas.DataFrame(index=idx1.union(idx2).union(idx3))

# create a (constant) Series for each sensor
df['Sensor1'] = df.index.to_series().apply(lambda x: 3 if x >= sensor1.Time.min() and x <= sensor1.Time.max() else numpy.NaN)
df['Sensor2'] = df.index.to_series().apply(lambda x: 2 if x >= sensor2.Time.min() and x <= sensor2.Time.max() else numpy.NaN)
df['Sensor3'] = df.index.to_series().apply(lambda x: 1 if x >= sensor3.Time.min() and x <= sensor3.Time.max() else numpy.NaN)

# plot
p = df.plot(ylim=[0, 4], legend=False)
p.set_yticks([1., 2., 3.])
p.set_yticklabels(['Sensor3', 'Sensor2', 'Sensor1'])

顺便问一下,你确定你的时间戳是2001年吗?这将使您的Sensor1图非常小。