# Plotting with gaps in x-axis data

13 views (last 30 days)
Matt on 3 Jul 2020
Edited: Matt on 10 Jul 2020
I am plotting data against time. The time variable has a uniform sampling rate, but there are times when there may be gaps in the data, for example, when something is powered off. These gaps can be any length of time. When you plot a variable against this time, if there is a gap between two points in time that is greater than the sampling rate, you naturally end up with a plot that has a single line between those two points. Visually, this can be deceiving and can make the gap difficult to identify at first glance, depending on the values of the other variable at those two points. Ideally, I would want to supress the line between those two points, leaving a visual gap. This could obviously be done manually by identifying gaps and then plotting different "chunks" of data with separate commands, but this is not practical beyond a one-off situation. You could potentially create a wrapper around the plot function and choose a time value that identifies gaps in the data that are "too large," plotting "chunks" of data defined in that manner. This would suffer from the use of a static value for that "too large" designation. Does anyone have any good ideas for handling the situation I described, or perhaps seen anything creative out there that would be useful? I realize I'm fighting against the nature of the plot function, but I thought someone else might have already thought of a creative way to address the issue.
Edit:Here's an example of the time vector for reference:
Let's say I have data that is logged at ~1 Hz and the time variable has a range of 10 hours. It might start out like this (I'll assume index 1 is time=0 for ease of dicussion):
[0, 1.00, 2.01, 3.02, 4.01, 5.01, 5.99, 6.98, 7.99, 9.01, 10.00...]
Then you might see a number of gaps of several seconds throughout the 10 hours, maybe a few gaps of several minutes, and a couple gaps of 30 or 45 minutes. Obviously, for the values that are ~1 second apart, I want to connect the data points with a line normally.
Thanks for the help.
Matt

madhan ravi on 3 Jul 2020
What about using nans as gaps?
Matt on 3 Jul 2020
I see that NaNs would break up the plot. I'm not sure how to implement that without doing something klunky like using the find function to find indices where the delta time is greater than some predetermined, constant value.

KSSV on 3 Jul 2020
It looks like you have a non continuous x-axis data. This problem occurs when you have such data and try to plot.
1. Make x-axis data continuous.
2. Initiate y-axes data to NaN's equaly in number to x-axis data.
3. Use ismemebr to get the existitng values of x-axis data. You can also use setdifference.
4. Fill the respective y-axis values with your available data. This will keep NaN's at the missing data.
5. Plot the above data. As you have NaN's in the y-axis data and x-axis data is continuous, you will not see a striaght line joining them. You will get gap due to NaN's.

Matt on 3 Jul 2020
Let's say I have data that is logged at ~1 Hz and the time variable has a range of 10 hours. It might start out like this (I'll assume index 1 is time=0 for ease of dicussion):
[0, 1.00, 2.01, 3.02, 4.01, 5.01, 5.99, 6.98, 7.99, 9.01, 10.00...]
Then you might see a number of gaps of several seconds throughout the 10 hours, maybe a few gaps of several minutes, and a couple gaps of 30 or 45 minutes. Obviously, for the values that are ~1 second apart, I want to connect the data points with a line normally.
When you say, "make the x-axis data continuous," are you suggesting I take the range (10 hours) and create a new time variable with the same range and dt = 1 second (exactly 1 second)? Using ismember or setdiff with this new time variable and the actual time variable will likely not result in much crossover, as there are not going to many values of time that are integers. Filling a corresponding new y-axis variable could not be done with the values in the original y-axis variable because the values would not be aligned with the new time variable; you would end up with mostly NaNs. I imagine you would have to interpolate.
Does this provide any additional clarify? The key is to ultimately end up with an automated process.
KSSV on 3 Jul 2020
That case you decide the time step where you want to give gaps....and make those values NaN's....you need not to give all the values in between nans...to give a gap the end value and first value is nan, it will not get joined while plot.
Matt on 3 Jul 2020
I'm not sure what the suggestion is here. Are you still suggesting creating a new, uniform time vector? As I said, if you do that, I don't see how you could use something like ismember or setdiff because you aren't going to get exact matches with the original time vector. You would have to interpolate new values of the y-axis parameter. Is that your suggestion? You could use find on diff(time) to find indices of gaps, but then you would have to insert a value in the time vector (and the y-axis parameter) at each of these indices and set the value to NaN. Then you increase the length of the vectors. Instead of inserting NaN values, you could overwrite individual values in the y-axis variable with NaNs where you want gaps, but then you lose data. Using find like that is pretty messy and I want to avoid overwritting data.
I understand the idea of using NaNs to break the lines in the plot. I'm just not sure of a clean way to do that in this situation. And I want to be able to essentially make this a wrapper around the plot function where I can provide the "gap value" as an input.
I am also looking for any creative alternatives that take a different approach. For example, many times, if you use the plot command and use markers and no line, you can get the right visual appearance, but if the variation in the data is too great or if the time scale is too small, it doesn't hold up.