使用 Matplotlib 绘图时跳过丢失的时间戳 Python
Skip missing timestamp when plotting with Matplotlib Python
我有一个实验数据集,每秒记录 10 个读数,即每分钟 600 个读数。数据是 1 个月的,但缺少一些日期,我假设那些日子的读数已关闭。
当我使用 matplotlib 绘制此读数与时间的图表时,会绘制一条线连接最后一个可用日期和下一个可用日期。
但是,我希望显示一个间隙而不是这条线,以便查看者清楚地知道那些日子的数据不可用。
我正在使用 Matplotlib 和 Python 3 绘图。
这是数据的样子
timestamp,x
2019-09-03 18:33:38,17.546
2019-09-03 18:33:38,17.546
2019-09-03 18:33:39,17.546
2019-09-03 18:33:39,17.555999999999997
2019-09-03 18:33:39,17.589000000000002
2019-09-03 18:33:39,17.589000000000002
2019-09-03 18:33:39,17.589000000000002
2019-09-03 18:33:39,17.589000000000002
2019-09-03 18:33:39,17.593
2019-09-03 18:33:39,17.595
2019-09-03 18:33:40,17.594
我认为在这种情况下,您应该通过以 10Hz
重新采样您的数据帧,将丢失的数据添加到您的数据帧中
df = pd.concat([pd.DataFrame([10]*600, index=pd.date_range(start='2018-01-01 00:00:00', periods=600, freq='0.1S')),
pd.DataFrame([20]*600, index=pd.date_range(start='2018-01-01 00:01:10', periods=600, freq='0.1S'))])
df2 = df.resample('0.1S').asfreq()
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(8,4))
df.plot(ax=ax1)
df2.plot(ax=ax2)
基于@Diziet Asahi 的回答的另一种选择是将其绘制为散点图而不是直线。这应该适用于单个 x 值的多个数据点。由于您的数据被大量采样,因此它可能具有与线条相似的视觉效果。
import matplotlib.pylab as plt
%matplotlib inline
import pandas as pd
# first bit of code copied from @Diziet's answer
df = pd.concat([pd.DataFrame([10]*600, index=pd.date_range(start='2018-01-01 00:00:00', periods=600, freq='0.1S')),
pd.DataFrame([20]*600, index=pd.date_range(start='2018-01-01 00:01:10', periods=600, freq='0.1S'))])
df2 = df.resample('0.1S').asfreq()
# plot three times, twice using different settings using the original data,
# once using resampled data
fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(8,4))
df.plot(ax=ax1)
df.plot(ax=ax2, marker='.', linestyle='')
df2.plot(ax=ax3)
我有一个实验数据集,每秒记录 10 个读数,即每分钟 600 个读数。数据是 1 个月的,但缺少一些日期,我假设那些日子的读数已关闭。 当我使用 matplotlib 绘制此读数与时间的图表时,会绘制一条线连接最后一个可用日期和下一个可用日期。
但是,我希望显示一个间隙而不是这条线,以便查看者清楚地知道那些日子的数据不可用。
我正在使用 Matplotlib 和 Python 3 绘图。
这是数据的样子
timestamp,x
2019-09-03 18:33:38,17.546
2019-09-03 18:33:38,17.546
2019-09-03 18:33:39,17.546
2019-09-03 18:33:39,17.555999999999997
2019-09-03 18:33:39,17.589000000000002
2019-09-03 18:33:39,17.589000000000002
2019-09-03 18:33:39,17.589000000000002
2019-09-03 18:33:39,17.589000000000002
2019-09-03 18:33:39,17.593
2019-09-03 18:33:39,17.595
2019-09-03 18:33:40,17.594
我认为在这种情况下,您应该通过以 10Hz
重新采样您的数据帧,将丢失的数据添加到您的数据帧中df = pd.concat([pd.DataFrame([10]*600, index=pd.date_range(start='2018-01-01 00:00:00', periods=600, freq='0.1S')),
pd.DataFrame([20]*600, index=pd.date_range(start='2018-01-01 00:01:10', periods=600, freq='0.1S'))])
df2 = df.resample('0.1S').asfreq()
fig, (ax1, ax2) = plt.subplots(1,2, figsize=(8,4))
df.plot(ax=ax1)
df2.plot(ax=ax2)
基于@Diziet Asahi 的回答的另一种选择是将其绘制为散点图而不是直线。这应该适用于单个 x 值的多个数据点。由于您的数据被大量采样,因此它可能具有与线条相似的视觉效果。
import matplotlib.pylab as plt
%matplotlib inline
import pandas as pd
# first bit of code copied from @Diziet's answer
df = pd.concat([pd.DataFrame([10]*600, index=pd.date_range(start='2018-01-01 00:00:00', periods=600, freq='0.1S')),
pd.DataFrame([20]*600, index=pd.date_range(start='2018-01-01 00:01:10', periods=600, freq='0.1S'))])
df2 = df.resample('0.1S').asfreq()
# plot three times, twice using different settings using the original data,
# once using resampled data
fig, (ax1, ax2, ax3) = plt.subplots(1,3, figsize=(8,4))
df.plot(ax=ax1)
df.plot(ax=ax2, marker='.', linestyle='')
df2.plot(ax=ax3)