I have a dataset from an experiment that records 10 readings per second, i.e. 600 readings per minute. The data is for 1 month but some dates are missing, I am assuming the reading was turned off on those days. When I plot a graph of this reading vs Time using matplotlib, a line is drawn connecting the last available date and next available date.

However, instead of this line, I want a gap to be shown so that it is clear to the viewer that data is unavailable for those days.
I am using Matplotlib and Python 3 to plot.
Here is how the data looks like
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
Another option based on @Diziet Asahi's answer is to plot as a scatter rather than a line. This should work with multiple data points for a single x-value. Since you data is very heavily sampled it may have a similar visual effect to a line anyway.
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)

I think in this case, you shoud add the missing data to your dataframe by resampling your dataframe at 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)

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With