0

Everyone wants to get rid of time gaps in plot, I want them to be shown!

So I have a table in my workspace (tableM) with two variables (Date and Temperature).
Date is in the "datenum" format. I have done a lot of preprocessing on the data and had to remove many rows. Thus, I have ended up with a date variable with gaps.

When I plot the Temperature through this code: plot(tableM.Temperature), MATLAB plots the temperature normally and it just connects the two points (before the missing date with after the missing date).

When I plot temperature through this code plot(tableM.Date,tableM.Temperature) and use the datetick function, I get a plot where the date is fixed in the horizontal axis and for the date gaps, MATLAB connects the two points (before and after the missing date) with a straight line over this gap period.

What I want is to get zero for the gap period instead of this straight line connecting these points.

How do I do this?

This is the example:

% Generating data:
Date = [datenum(now-2000):datenum(now+2000)]';
Temperature = rand(4001,1); Temperature = sort(Temperature);
% Creating gaps:
Date(500:600) = []; Temperature(500:600) = [];
Date(500:600) = []; Temperature(500:600) = [];
Date(1500:2000) = []; Temperature(1500:2000) = [];
Date(1500:2000) = []; Temperature(1500:2000) = [];
% To make illustration mroe understandable:
Temperature(601:1499) = 1.8 * Temperature(601:1499);
% Normal plot:
figure; plot(Date,Temperature)
ax = gca; ax.XTickLabelRotation = -45; ax.XTick = Date(1,1):200:Date(end,1);
datetick('x',20,'keepticks','keeplimits'); ylabel('Temperature (C)'); axis tight
% Scatter plot:
figure; scatter(Date,Temperature,'.')
ax = gca; ax.XTickLabelRotation = -45; ax.XTick = Date(1,1):200:Date(end,1);
datetick('x',20,'keepticks','keeplimits'); ylabel('Temperature (C)'); axis tight

IMPORTANT: My "Date" vector is on "hourly" basis. I don't know if that creates any particular difference between the datenum values in the Date vector that could be useful.

Image1: Normal and Scatter Plots

Image2: Preferred Plot

PM0087
  • 123
  • 1
  • 9
  • Wouldn't it be better to use a scatterplot instead of a line then? – Eli Sadoff Dec 08 '16 at 19:18
  • Thanks for the super quick reply! That's also nice, though, it doesn't give me zero values. The plot looks just cut. Of course I could figure out the points that the data are abrupt, simply add NaNs. But I have to do this for many objects which have different time gaps (one day, one month), and more than one gap. – PM0087 Dec 08 '16 at 19:27
  • I would personally do a scatterplot with some sort of regression. – Eli Sadoff Dec 08 '16 at 19:31
  • Setting unknown data points to 0 is no more accurate than having whatever matlab decides them to be. – Eli Sadoff Dec 08 '16 at 19:31
  • It's helpful if you include initializations for your variables so people that are interested in helping can run it and see what you mean. I personally don't fully understand what you see or what you want, so for me it would be helpful if you added this information to your original post. – Franz Hahn Dec 08 '16 at 20:23
  • @FranzHahn: I updated the question with the code and plots. – PM0087 Dec 09 '16 at 11:05

0 Answers0