如何将时间范围绘制为 Pandas 或 MatPlotLib 中的值
How to plot a time range as a value from Pandas or MatPlotLib
我有几个包含时间序列数据的数据帧,我想为每个数据帧创建时间范围跨度的简单可视化。
由于我无法用代码生成它,所以我提供了一个草图来说明我的目标。
下面是一些创建三个 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' 列,并将整数分配给 'value' 列(即传感器 1 将 Int 1 广播到 'value' 列, sensor2 获取 Int 2 广播等),然后合并这些数据帧。
但这会导致 'Time' 列中出现大量重复值,而 'value' 列中出现 Nan 值。
我 运行 不知道如何让它发挥作用。
编辑:更正了代码块中偷偷摸摸的“2001”时间戳;-)
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 图小到看不见。
我有几个包含时间序列数据的数据帧,我想为每个数据帧创建时间范围跨度的简单可视化。 由于我无法用代码生成它,所以我提供了一个草图来说明我的目标。
下面是一些创建三个 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' 列,并将整数分配给 'value' 列(即传感器 1 将 Int 1 广播到 'value' 列, sensor2 获取 Int 2 广播等),然后合并这些数据帧。
但这会导致 'Time' 列中出现大量重复值,而 'value' 列中出现 Nan 值。
我 运行 不知道如何让它发挥作用。
编辑:更正了代码块中偷偷摸摸的“2001”时间戳;-)
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 图小到看不见。