Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip missing timestamp when plotting with Matplotlib Python

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.

enter image description here

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
like image 938
Harsha Varwani Avatar asked Sep 23 '26 06:09

Harsha Varwani


2 Answers

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)

enter image description here

like image 185
Robert King Avatar answered Sep 25 '26 20:09

Robert King


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)

enter image description here

like image 28
Diziet Asahi Avatar answered Sep 25 '26 19:09

Diziet Asahi