gnuplot X and Y values in separate lines - date

I'm trying to gnuplot a data set like this
Di 4. Dez 19:10:09 CET 2012
Garbage
description: 10.5 description2: 22.3
Mi 5. Dez 01:51:15 CET 2012
Garbage
description: 10.5 description2: 22.4
Those 3-line-blocks repeat several times. I want to use the time from line 1,4,7 etc. to provide the date (as X-value) for the actual data in lines 3,6,9 etc. (as Y-values) every other column. In the end the plot should be composed by the following lines (in [X,Y]-format) [1,3],[1,6],[1,9],[4,3],[4,6],[4,9] etc.
From what I read here it's possible to use every but I can't figure out how.
EDIT: I solved the timefmt issue below thanks to user1901493's tip. Disregard the issue below (didn't delete it there for reference).
A test with just the first line with set timefmt "%d %b %H:%M:%S" and using 2:6 doesn't work either, complaining about a bad month abbreviation. This should work though. This could be part of the problem though.

The bad month abbreviation might come from the fact that gnuplot does not understand the german Dez. abbreviation, so try again with dec instead of Dez.
Secondly, I think gnuplot only uses data in columns, so a file with your format:
x1
...
y1 z1
x2
...
y2 z2
might not work at all. I suggest that you rearrange your data file with something like grep so that your datafile looks like this:
x1 y1 z1
x2 y2 z2

Related

Plot Two Regression Lines on Same Scatter Plot By Year: X-Axis Date MM/DD

I have a scatter plot of calls / time. My x variable is the date (Day/Month) and my Y variable is a number of calls on each date. I would like to plot two regression lines using PROC SGPLOT REG, one for 2019 and one for 2020. However, when I try to do this, all I get is a regular scatter plot with no regression lines. Here is my code:
proc sgplot data=intern.bothphase1;
reg x=date y=count / group=Year;
label count="Calls Per Day" year="Year";
Title "Comparison of EMS Calls per Day 1/1 - 3/31 in 2019 vs.
2020";
run;
The scatter plot comes up without issue (2019 and 2020 values in different colors) but I want to see how the trends differed between the two time periods, so I really want to get the regression lines on there. Can anyone help?
I imagine this has to do with the fact that I concatenated my day and month with a / so it is a character variable and so SAS cannot calculate the regression. I did this so I could use year as a class variable. I still have the original date variable in my table, is there a way I could get SAS to give me the month/day from that as a numeric variable?
Thanks!
EDIT: I used a date value in SAS and changed the format to mm/dd, but this doesn't help because the regression lines are just on either end of the graph rather than overlapping (picture attached). what I want is to have the regression lines overlap for the same time period 2019 vs. 2020 This is because SAS dates correspond to numbers from 1/1/1960. What I want is the mm/dd to correspond to numbers 1-365 so I get two overlapping regression lines to show how the trends changed from one year to the next. Anyone know how I can do this?
So two steps here: first, you need to generate a "day" value that's 1-365... so let's just subtract out 01JAN from the day value.
data have;
do date = '01JAN2019'd to '31DEC2020'd;
count = 25+2*rand('uniform');
year = year(date);
if month(date) le 3 then output;
end;
format date date9.;
run;
data adjusted;
set have;
date_fixed = date - intnx('year',date,0,'b') + 1; *current date minus jan 1 plus 1 (otherwise off by 1);
format date_fixed date5.; *this does not actually affect the graph axis, oddly;
run;
proc sgplot data=adjusted;
reg x=date_fixed y=count / group=Year;
xaxis valuesformat=date5.; *this seems to be needed for some reason;
label count="Calls Per Day" year="Year";
Title "Comparison of EMS Calls per Day 1/1 - 3/31 in 2019 vs.
2020";
run;
Then we add the xaxis line because for some reason it won't obey the DATE5. format (could also use MMDDYY5. as Reeza noted in comments, but we can force it to here.
Here is what I get. You can use other axis options to further limit things, so for example 01APR doesn't show up.
)

Plotting in Matlab with datetime in X axis

I am trying to plot a graph with temperature values(Y) vs. datetime(X).
The datetime is constituted of many equal dates, without hour associated. For example, I would have 1440 values with 2018/01/11, then other 1440 values with 2018/01/12.., whereas for each point I have a different temperature value.
This is what it should look like:
But this is how it ended up when I use plot(temperature, date):
All the values for the same day are accumulated as it was a stem ..
I would need it to map point 1 of Y with point 1 of X, and so on, without recognizing the X axis as a datetime, but instead to recognize it as a string. I don't know if I am explaining it right.
But to give a simple example, basically would be something like:
Y = [1, 2, 3, 4, 5]
X = [2016, 2016, 2016, 2016]
And all the 2016 should be different sequence values on the graph.
Would someone be able to help me, please?

How to fill the last observations with retime in matlab?

I am interpolating variables from quarterly to monthly frequency in MATLAB. However, when I use retime it doesn't go as far as the end of the sample but it stops 2 months before.
Let me give you an example:
T = datetime(2002,01,01):calquarters:datetime(2019,12,01);
TT = timetable(T', randn(72,1))
x = retime(TT, 'monthly', 'spline') % interpolate
As you can see it gives me back 214 observations rather than 216, November and December 2019 are missing.
How can I fix it?
Thanks!
I don't have enough reputation to add a comment, but TT having 72 quarters instead of 73 means that you are actually storing dates from 1st January 2002 to 1st October 2019 - as the next quarter would start from 1st January 2020, which is then not included in your original array (you can check this by printing TT and checking if this date is included or not).
If this is the case, there is no way for retime to interpolate the missing months, as they aren't in the original matrix (that is, retime cannot interpolate from October to January, since there is no such thing in TT).
Replacing datetime(2019,12,01) with datetime(2020,01,01), as well as replacing randn(72,1) with randn(73,1), might solve your issue.

gnuplot overlay same time different day plots?

I have two data files of two days and same time
date1data.csv
2018.02.03.18:23,4
2018.02.03.19:23,22
2018.02.03.20:23,12
2018.02.03.21:23,3
2018.02.03.22:23,16
date2data.csv
2018.02.04.18:23,1
2018.02.04.19:23,5
2018.02.04.20:23,22
2018.02.04.21:23,5
2018.02.04.22:23,14
If I plot them I get this plot where the data is plotted one day after another.
How can I plot them with respect to the time only and not the date so that they overlay at the same time? Is this possible? I dont want to plot them in multiple windows.
Thats the code I used for plotting:
set datafile separator ","
set terminal pngcairo size 500,200 enhanced font 'Verdana,10' linewidth 1
set output 'test.png'
set xdata time
set timefmt "%Y.%m.%d.%H:%M"
set format x "%H:%M"
plot 'date1data.csv' using 1:2 w lines lw 2 t "day1", 'date2data.csv' using 1:2 w lines lw 2 t "day2"
You can replace using 1:2 with a function that drops the date part of the time string that is column 1:
set timefmt "%H:%M"
f(v) = substr(stringcolumn(v),12,16)
plot 'date1data.csv' using (f(1)):2 w lines lw 2 t "day1",\
'date2data.csv' using (f(1)):2 w lines lw 2 t "day2"
Here is a solution that uses the stats command to find the earliest time in the two data files, and shifts the times of the second file so that aligns with the earliest time in the first file. We have to work around a limitations of the stats command because it does not work in timedata mode, but we can work around that by doing the time-to-seconds conversion ourselves:
set datafile separator ","
fmt = "%Y.%m.%d.%H:%M"
stats "date1data.csv" u (strptime(fmt,strcol(1)))
t1min = STATS_min
stats "date2data.csv" u (strptime(fmt,strcol(1)))
t2min = STATS_min
set xdata time
set format x "%H:%M"
plot 'date1data.csv' using (strptime(fmt,strcol(1))):2 w lines lw 2 t "day1", \
'date2data.csv' using (strptime(fmt,strcol(1))-t2min+t1min):2 w lines lw 2 t "day2"
This approach has the advantage that it should work even if the data crosses date boundaries.
You can just remove the date part from your data by subtracting midnight:
t(x) = x - 24*3600*(floor(x/(24*3600))
plot dataf1 us (t($1)):2, dataf2 us (t($1)):s
This actually works 100% correctly because unix time ignores leap seconds, your computer clock just adjusts itself when one occors.

MATLAB Change numbers to date

I have time set up as serial dates. Each number corresponds to a day, in order, from 20100101 to 20130611. How do I convert the serial date to a date in the format month-year? I need this because I want to plot data and need the x axis to show the date.
Thanks!
The first step is to convert your date-format into one of the standard Matlab date formats. The best format to use for plots is the "serial date format". The numbers itself are a bit awkward, since they represent the "amount of time after 0/0/0000, in days", which is a huge number. Also, this date actually never existed, making it really weird when you want to work with dates that are BC.
However, the conversion is easy, since your format also counts the days, but you count after 31st of December, 2009. You can convert this using
numeric_date_vec = datenum(2009, 12, 31) + x;
You then plot your data using
plot(numeric_date_vec, y)
and you let Matlab add the date-ticks automatically by calling
datetick('mmm yyyy')
The problem is, the ticks do not update after zooming in. You can either call
datetick('mmm yyyy','keeplimits')
again, after each zooming or panning, or you download datetickzoom from the Matlab file exchange. It takes the same arguments as datetick, but it hooks into the zoom function and updates the ticks automatically.
Edit:
Sometimes, the dateticks are not spaced in any sensible way, then you can either try to zoom in and out a little until it snaps to something good, or you have to set the ticks manually:
% Set ticks to first day of the months in 2010
tick_locations = datenum(2012,[1:12],1);
% Set ticks on x-axis
set(gca, 'XTick', tick_locations)
% Call datetick again to get the right date labels, use option "keepticks"
datetick('mmm yyyy','keeplimits', 'keepticks')
You might have to modify the tick_locations = datenum(2012,[1:12],1) a bit to get the ticks that you want. For instance, you can use
tick_locations = datenum(2012,[1:2:25],1)
to get every second month between Jan 2012 and Jan 2013.
For day number n use
datestr(datenum(2009, 12, 31) + n, 'yyyy-mm')
for example
>> datestr(datenum(2009, 12, 31)+365, 'yyyy-mm')
ans =
2010-12
>> datestr(datenum(2009, 12, 31)+366, 'yyyy-mm')
ans =
2011-01