I'm trying to plot a graphic that displays values against moments of time.
For this, I have an array of time instants (in Epoch) and an array of values.
I've already been able to plot the graphic normally using the raw time (as Epoch). The problem is specifically in the conversion of the axis time format.
hold on;
plot(horizontal, pre_X(:,4), 'b-');
xt = get(gca, 'xtick');
set(gca, 'xticklabel', sprintf('%d|', xt));
datetick ("x", "dd/mmm/YY HH:MM");
yt = get(gca, 'ytick');
set(gca, 'yticklabel', sprintf('%d|', yt));
hold off;
The datetick function was supposed to be able to transform these Epoch times into nicely formatted ones, but I am not getting the expected result. Instead, all time instants get labeled as the same (01/JAN/00 00:00) which is weird.
The plot without the
datetick ("x", "dd/mmm/YY HH:MM");
line works fine, but gives the time information in Epoch, which is not what I intend to.
Any help would be appreciated!
NOTE: If the right function to do what I intend to turns out not to be "datetick", also please let me know! All I need is to get the X axis to be formatted nicely into readable time.
EDIT: By Epoch Time, I mean Unix Time.
Lacking more info, I'm going to assume that by Epoch time you mean posix, or Unix time
I would expect that to then be represented as a 32-bit integer, and should be the number of seconds counted from 'zero time' (as described in the Wiki linked) (it may also be a floating point number including fractional seconds using the same scale).
According to the Matlab help for datetick, it expects the axis data to be "serial date numbers, as returned by the datenum function". For compatibility, octave likely expects the same, although the datetick function reference does not state such explicitly.
The datenum "serial date number" format is another serial representation of time, but it has a different scale and reference than Epoch/posix/unix time. According to the datenum function description, it's serial definition is "date/time input as a serial day number, with Jan 1, 0000 defined as day 1".
That's a long way of saying you're time is probably in units of seconds, whereas datenum expects units of days.
Now, you can probably address this a couple ways. you can covert all of your times to the datenum scale before plotting. from this example, something like this would work in Matlab:
datetime(1470144960, 'convertfrom','posixtime')
According to bug #47032 datetime has not yet been implemented in Octave, but that bug report does link to a github repository with a datetime class implementation (under the inst/ folder).
To manually convert from unix to matlab time, you could convert following this example from the Mathworks File exchange:
unix_epoch = datenum(1970,1,1,0,0,0);
matlab_time = unix_time./86400 + unix_epoch;
(assuming your x-axis data is the unix_time variable)
Once you get your data into the 'datenum' scale, datetick should perform correctly.
Related
I am new to Pentaho, so please be gentle.
I am, perhaps naively, wanting to use a Formula to convert a six-character string in the form YYYYMM to the date representing the final day of that month.
I imagine doing this step by step using successive lines of the Formula: checking that the string is of the correct length and, if so:
extracting the year and converting it to integer (with error checking)
extracting the month and converting it to integer (also with error checking)
converting ([year], [month], 1) to a date (the first of the month)
adding a month
subtracting a day
Some of those steps may be combined but, overall, it relies on a succession of steps to achieve a final result.
Formula does not seem to recognise the values achieved along the way though, at least not by enclosing them in square brackets as you do with fields from previous objects in the mapping.
I suppose I could have a series of Formula objects one after the other in the mapping but that seems untidy and inefficient. If a single Formula object cannot have a series of values defined on successive lines, what is the point of even having lines? How do I use a value I have defined on a previous line?
The formula step isn’t the best way to achieve that. The resulting formula will be hard to read and quite cumbersome.
It’s better (and faster) to use a calculator step. A javascript step can also be used, and it will be easier to read, but slower (though that probably won't be a major issue).
So, one way forward is to implement this on a calculator step:
Create a copy of your string field as a Date
Create 2 constant fields: 1 and -1
Add 1 month to the date field
Subtract 1 day to the result
Create a copy of the result as a string.
See screenshot:
I am working to create a nice looking plot of the survfit return in R. The default time unit on the x-axis is a long integer, which I am assuming is seconds or milliseconds. This persists no matter what the unit of time is that is originally passed to the Surv(time, event) function. I have tried passing the start and stop times as POSIXcs dates as well as in milliseconds. Does anyone know how to indicate to either survfit or plot.survfit that I would like the time result to be in either Days, Hours format or HH:MM format? Thanks for your help.
survival <- survfit(Surv(time=starteventtime,event=endevent,time2=stoptime)~1)
plot(survival)
the current plot rendered
I have time series data that I'm trying to analyse in R. It was provided as a CSV from excel, which I subsequently read as a data.frame all. Let's say it has two columns: all$date and all$people, representing the count of people on a particular date. The frequency is hence daily.
Being from Excel, the dates are integers representing the number of days since 1900-01-01.
I could read the data as people = ts(all$people, start=c(all$date[1], 1), frequency=365); but that gives a silly start value of almost 40000 because the data starts in 2006. The start parameter doesn't take a date object, according to ?ts, so I can't just use as.Date():
ts - ...
start: the time of the first observation. Either a single number
or a vector of two integers, which specify a natural time unit and
a (1-based) number of samples into the time unit. See the examples
for the use of the second form.
I could of course set start=1, but it's a bit painful to figure out what season we're in when the plot tells me interesting things are happening around day 2100. (To be clear, setting frequency=365 does tell me what year we're in, but isn't useful more precise dates). Is there a useful way of expressing the date in ts in a human-readable form so that I don't have to keep calling as.Date() to understand when the interesting features are happening?
I'm working on 16 world indices over three year and i want to make a plot from these 16 indices.
all<-read.table("C.../16indices.txt")
dimnames(all)[[2]]<-c("Date","BEL 20","CAC 40","AEX","DAX","FTSE 100","IBEXx 35","ATX","SMI","FTSE MIB","RTX","HSI","NIKKEI 225","S&P 500","NASDAQ","Dow Jones","BOVESPA")
attach(all)
Problems
My dates are written in the form "2009-01-05". I want only "2009" to appear otherwise i would have to many jumps.
For example the prices from the BOVESPA go from 40.000,15 to 60.000,137. How do I get nice y-labels? For instance 40.000, 45.000,...,60.000.
How do i get 16 of these plots in one nice figure/plot?
I'm not used to work with R. I tried something like this but that didn't work...
plot(all[1,],all[,2])
Biggest problem is no sample data> Here is advice based on guesswork:
I tried something like this but that didn't work... plot(all[1,],all[,2])
You need to format your date values as R Date class. If they are in YYYY-MM-DD format it will be as simple as:
all$Date <- as.Date(all.Date)
To your specific questions:
1) My dates are written in the form "2009-01-05". I want only "2009" to appear otherwise i would have to many jumps.
You will need to suppress axis plotting in the plot call and then need to add an axis() call.
2) For example the prices from the BOVESPA go from 40.000,15 to 60.000,137. How do I get nice y-labels? For instance 40.000, 45.000,...,60.000.
You appear to be in a European locale and that mean your initial read.table call probably mangled the data input and you need to read the documentation for read.csv2 which will properly handle the reversal of the decimal point and comma meanings for numeric data. You should also use colClasses.
3) How do i get 16 of these plots in one nice figure/plot?
You should probably calculate ratios from an initial starting point for each series so there can be a common scale for display.
I am trying to plot data from a simulation that tracks simulation time in (hours):(minutes):(seconds) format, but does not turn (hours) into days - so (hours) can be in the hundreds. When gnuplot plots data by time, however ("set xdata time"), it only plots up to 99 hours in one continuous plot; after that, it loops back around and starts overplotting hour 100+ near the beginning (and even then, does weird stuff). Does anyone know why this happens and/or how to get around it?
I also looked into reading the components of the time column (which is the 3rd field of data on each line, but not necessarily a fixed number of characters into the line) in as 3 simple numbers (integers), then converting to a real number, which happens to be a decimal version of the time (e.g., 107:45:00 -> 107.75), which would be fine for the plot, but I haven't been able to figure out how to get gnuplot to do that, either.
Any other ideas are welcome. (I would rather not alter the original file, due to the additional complexity of multiple versions of each file, having to teach others how to convert the file and how to figure out the plot didn't work because they didn't convert the file, etc.)
Version 2 of MathGL (GPL plotting library) have time ticks which can be set as you want (using standard strftime() format). However it is in beta version now -- stable version should appear at October 2011.