Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Matplotlib Python打印时跳过丢失的时间戳_Python_Pandas_Matplotlib_Python Datetime - Fatal编程技术网

使用Matplotlib Python打印时跳过丢失的时间戳

使用Matplotlib Python打印时跳过丢失的时间戳,python,pandas,matplotlib,python-datetime,Python,Pandas,Matplotlib,Python Datetime,我有一个实验数据集,每秒记录10次读数,即每分钟600次读数。数据是1个月的,但有些日期不见了,我假设这些天的读数是关闭的。 当我使用matplotlib绘制此读数与时间的关系图时,会绘制一条连接上一个可用日期和下一个可用日期的线 但是,我不希望显示这一行,而是希望显示一个间隙,以便查看者能够清楚地看到这些天的数据不可用 我正在使用Matplotlib和Python 3进行绘图 下面是数据的样子 timestamp,x 2019-09-03 18:33:38,17.546 2019-09-03

我有一个实验数据集,每秒记录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)

是否尝试过滤空时间戳值?Hi@FBruzzesi数据集根本不包含这些时间戳值。例如,行i包含日期为2019-09-12的时间戳,行i+1直接包含日期为2019-09-18I的时间戳。我认为resample()方法需要时间戳作为日期时间索引。在这种情况下,由于时间戳中没有毫秒值(“.f”),因此至少有10个时间戳具有相同的值“%Y-%m-%d_%H-%m-%S”。将此列设置为索引将导致重复值轴的错误。我使用了一个方形标记,并将其大小调整了一点
ax1。绘图(x,y1,color=color1,label=y1\u label,marker='s',linestyle='',markersize=1.5)
,它看起来与折线图类似