How to remove gaps in timeseries plot to have a continuous graph

Hello Everyone,
I have a timeseries data from a vehicle having gaps in it. FOr example, when the vehicle was running, there is data, but no data is available when the vehicle is turned off. When i plot now, the missig data is filled witha line connecting last measured data to the next measured data (see figure). How can i remove that line and instead have a continuous line.
I highly appreciate for any efforts.

3 Comments

No easy way to do it since you concatenated your data.
  • One way is to plot each data set individually so that they are only connected to each other. You can do it by seperating your data by checking the difference in their time values and then plotting
  • Another alternative is to use ( '.' )argument in your plot function, which will create a dot at each data point without connecting them.
"How can i remove that line and instead have a continuous line."
It is already a continuous line.
Your requirement is unclear.
Hi Stephan, sorry for my miscommunication. I just want to chop that connecting line and join the data before and after. I hopw thins is a better explaination of the requirement.

Sign in to comment.

Answers (2)

Include a NaN in the data list between each data set.
Hello Sachin ,
It appears you are trying to handle gaps in your time series data and prevent MATLAB from connecting the missing data points with a line,
you can try replacing the gaps with NaN values. When you plot the data, MATLAB will automatically break the line at these NaN values, resulting in a plot where only continuous data points are connected.
Here's an example using some sample data:
% Sample data with gaps
time = [1, 2, 3, 7, 8, 9, 15, 16, 17]; % Time points
data = [10, 12, 15, 20, 18, 22, 25, 24, 27]; % Corresponding data points
% Introduce gaps in the data
data_with_gaps = data;
data_with_gaps([4, 7]) = NaN; % Assuming data points at time 7 and 15 are missing
% Plot the data
figure;
plot(time, data_with_gaps, '-o');
xlabel('Time');
ylabel('Data');
title('Time Series Data with Gaps');
grid on;
This approach might help, that your plot accurately reflects the discontinuities in your data, providing a clearer visualization of periods where the vehicle was turned off.
you can refer to the following MathWorks documentation to know more
Hope this helps you in moving forward

5 Comments

Hello Pavan, thank you for taking time to have a look on my problem.
This solution was proposed by many, however, it still leaves a blank where NaN values exist. What i want is not to have a gap at all.
"What i want is not to have a gap at all."
How exactly?
Do you want to somehow shift those sample times so that there are no gaps (note: your datestamps will then be mostly incorrect/meaningless) ? You could do that by converting to duration (e.g. elapsed time since the first data sample) and shifting all timestamps after a gap greater than some delay that you define.
To have a continuous plot without any gaps, you need to have data points at every time step. If there are missing data points, one way to fill those gaps is by interpolating the missing values.
something like this
time_full = 1:17; % Full time range
data_interpolated = interp1(time(~isnan(data_with_gaps)), data_with_gaps(~isnan(data_with_gaps)), time_full, 'linear');

Sign in to comment.

Products

Release

R2024a

Asked:

on 31 Jul 2024

Commented:

on 31 Jul 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!