i have a graph to plot, with dates in each axis, in x axis i have a date that represent the date pictures of taks was taken, and in the y-axis i would like to put the finish dates of that task, my query returns something like this
Task PictureDate FinishDate
A 01-03-2010 20-05-2010
A 08-03-2010 20-05-2010
A 15-03-2010 22-05-2010
B 01-03-2010 12-06-2010
B 08-03-2010 13-06-2010
B 15-03-2010 14-06-2010
I can not put the Finishdate in y-axis, is there a workaround i can use to display y-axis label as date?
I don't know if it could apply for this, but in .Net we can use a DateTime as a double by doing.
Datetime dt = new DateTime()
double l = dt.toOADate();
dt = Datetime.FromOADate(double l);
Maybe by giving a format or axistype or something like this you can do it.
Related
I am new to python programming, and have been trying to put together a plot of numbers against dates. The only problem is, when I go to plot it individually seems to show every single date in my range of dates. Consequently the x axis is completely illegible. Is there some way to keep all my data points, but have the x axis only display dates by the month, or something similar?
The code, with some sample data, is as below:
import datetime as dt
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
dates=['1/08/2015', '2/08/2015', '1/09/2015', '2/09/2015', '3/09/2015','4/09/2015', '5/09/2015', '1/10/2015', '2/11/2015', '3/11/2015', '4/11/2015', '5/11/2015', '1/12/2015', '2/12/2015', '1/01/2016', '2/01/2016', '3/01/2016', '1/02/2016', '2/02/2016', '3/02/2016', '4/02/2016', '1/03/2016', '6/03/2016', '1/04/2016', '2/05/2016', '1/06/2016', '1/07/2016', '2/07/2016', '3/07/2016', '4/07/2016', '5/07/2016', '1/08/2016', '2/08/2016', '3/08/2016']
converteddates= [dt.datetime.strptime(d, '%d/%m/%Y').date() for d in dates]
data=range(len(converteddates))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.plot(converteddates,data)
plt.gcf().autofmt_xdate()
plt.show()
You should be using MonthLocator (DayLocator's job is to put ticks at everyday):
plt.gca().xaxis.set_major_locator(mdates.MonthLocator())
plt.plot(converteddates,data)
plt.gcf().autofmt_xdate()
But a better approach might be:
fig, ax = plt.subplots()
ax.plot_date(converteddates, data)
and if it still doesn't like your dates, then set the ticks and labels underneath:
ax.set_xticks(converteddates)
strdates = [d.strftime("%d/%m/%Y") for d in converteddates]
ax.set_xticklabels(strdates, rotation="45")
I am trying to plot the fit function (col 3 versus col 7) for my dataset given below
28-08-1991 20-12-1992 24-04-1992 -263347200 -221875200 -242611200 0.060859
20-12-1992 02-09-1993 27-04-1993 -221875200 -199756800 -210816000 0.064681
10-09-1996 13-09-1997 13-03-1997 -104371200 -72576000 -88473600 0.095728 ......
I would like my script to plot the fit only for certain range of dates, say between 1995 to 2003. I am unable to get the range function right since the gnuplot handle the dates past the year 2000.
Please find my code below.
set xdata time
set timefmt "%d-%m-%Y"
set xrange ["01-01-1991":"01-01-2015"]
set yrange [0.0:0.4]
offset = 24*60*60
f(x) = 1.e-7*m*(x-offset)+c
fit f(x) "file" using 3:7 via m,c
set fit logfile 'file.log'
plot '(x < "24-02-1993" ) ? f(x) : 1/0' title "Best fit" with lines ls 5
The variable x in plot will contain the time in the form of seconds since 1/1/1970. So in order to compare it with a date, you need to convert that date to the same format with strptime.
If you additionally remove the ' around the function to plot in your MWE, you get:
plot (x<strptime("%d-%m-%Y","24-02-1993"))?f(x):1/0 title "Best fit" with lines ls 5
Note that you've fitted f(x) over the whole dataset, not over dates before 24-02-1993, it's only the plot which is restricted to these dates.
I have the following plot created from this very simple script.
clear;
format long g
Ts = ncread('EURUSD_1 hour_30_D_2015-11-03_02-52-36-PM.nc', 'TimeStamp');
Obs = ncread('EURUSD_1 hour_30_D_2015-11-03_02-52-36-PM.nc', 'Close');
plot(datenum(Ts), Obs);
datetick('x', 20);
However, I get this error:
>> netcdfExample
Error using datevecmx
Date number out of range.
Error in datevec (line 303)
[y,mo,d] = datevecmx(t);
Error in dateTickPicker (line 85)
[y,m,d] = datevec(x);
Error in datetick>bestscale (line 292)
[labels,format] = dateTickPicker(axh,[xmin,xmax],dateform,dateChoice,axVal);
Error in datetick (line 251)
ticks = bestscale(axh,ax,vmin,vmax,dateform,dateChoice);
Error in netcdfExample (line 9)
datetick('x', 20);
>>
When I just try something simple like this
>> formatOut = 'mmmm-dd-yyyy';
>> str = datestr(6.35821236e+17,formatOut,'local')
I get this error. I wonder if these encoded date numbers are wrong?
Error using dateformverify (line 28)
DATESTR failed converting date number to date vector.
Error in datestr (line 194)
S = dateformverify(dtnumber, dateformstr, islocal);
Caused by:
Error using datevecmx
Date number out of range.
The datetimes/close look like this (in the .csv vesion)
9/22/2015 16:15 1.11255
9/22/2015 17:00 1.11305
9/22/2015 18:00 1.112
9/22/2015 19:00 1.1107
9/22/2015 20:00 1.1123
The date is from hourly intra-day data (netcdf format) that I read from a file into Matlab :
As you can see, the x-axis are numbers in 10^17 format instead of System.DateTime from C#. Also, please note that since the markets close and reopen, there is a line joining the plot between when the market closes, and reopens.
How do I:
Get rid of the date gap. I want the data to be next to its neighbor.
Make the x-axis tick marks be something resembling a date/time?
The y axis data has five decimal places of precision. How do I ask the plot to show more precision on the y-axis tick marks?
I sort of figured out a workaround. In the C# program, instead of using a Datetime, I convert the Datetime to a double. Then this program works fine
Ts = ncread('EURUSD_1 hour_30_D_2015-11-03_06-41-44-PM.nc', 'TimeStamp');
Obs = ncread('EURUSD_1 hour_30_D_2015-11-03_06-41-44-PM.nc', 'Close');
plot(Ts, Obs);
datetick('x', 'mm-dd-yy HHPM');
I still don't know how to add precision to the y axis, and how to remove the gaps in time on the x-axis. It is interesting because if I just say
plot(Obs)
The plot looks correct on the y-axis but the x-axis is numbers. If I add
plot(Ts, Obs)
datetick('x', 'mm-dd-yy HHPM');
Then the x-axis is correctly "indexed" by date/time, but I suspect that Matlab is honoring the gaps in time so the plot looks "jumpy" and disconnected.
I am using MATLAB R2015b. I am trying to import a excel file full of dates using xlsread('filename.xls'). The dates looks like the followings:
02/01/1996
03/01/1996
04/01/1996
05/01/1996
08/01/1996
then I want use datevec to separate the day month and year. for date = '02/01/1996'(January 2nd 1996), datevec gives Y= 1996, M = 2, D= 1,H=0 MN=0 S=0. For date '29/12/2000' (Dec 29 2000), datevec gives Y = 35, M=5 D =23 H=0 MN=0 S=0.
I tried to change the date format in excel, but it still does not work. Can anyone let me know how can I fix this please?
DateVector = datevec(DateString,formatIn)
As per the documentation. Set your formatIn correct:
DateString = {'16/09/2007';'14/05/1996';'29/11/2010'};
formatIn = 'dd/mm/yyyy';
datevec(DateString,formatIn)
Since MATLAB is an American program all their defaults are American (hence you're not able to call your colourbar as you want to). You just have to select a different date format.
I have a table with dates (and other things), which I have extracted from a CSV file. In order to do some processing of my data (including plotting) I decided to convert all my date-strings to date-numbers (below for simplicity reasons I will exclude all the rest of the data and concentrate on the dates only so don't mind the step from dates to timetable and the fact that it can be omitted):
dates = [7.330249777777778e+05;7.330249291666667e+05;7.330246729166667;7.330245256944444;7.330246763888889;7.330245284722222;7.330245326388889;7.330246625000000];
timetable = table(dates);
timetable
_________
7.330249777777778e+05
7.330249291666667e+05
7.330246729166667
7.330245256944444
7.330246763888889
7.330245284722222
7.330245326388889
7.330246625000000
I'm facing the following issue - based on the time during the day I want to tell the user if a date is in the morning (24-hours scale: 5-12h), noon (12-13h), afternoon (13-18h), evening (18-21h), night (21-5h) based on the date I have stored in my table. In case I had a date-vector (with elements: year,month,day,hour,minute,second) it would be pretty straight forward:
for date = 1:size(timetable)
switch timetable(date).hour
case {5,12}
'morning'
case {12,13}
'noon'
case {13,18}
'afternoon'
case {18,21}
'evening'
otherwise
'night'
end
end
With 7.330246729166667 and the rest this is not that obvious at least to me. Any idea how to avoid converting to some other date-format just for this step and at the same time avoid some complex formula for extracting the required data (not necessarily hour only but I'm interested in the rest too)?
One unit in Matlab serial dates is equivalent to 1 day, i.e. 24 hours. Knowing this, you can bin the fractional part of the the dates within the intraday buckets you defined (note that your switch will only work for values exactly equal to the case lists):
bins = {'morning', 'noon', 'afternoon', 'evening', 'night'};
edges = [5,12,13,18,21,25]./24; % As fraction of a day
% Take fractional part
time = mod(dates,1);
% Bin with lb <= x < ub, where e.g. lb = 5/25 and is ub = 12/24
[counts,~,pos] = histcounts(time, edges);
% Make sure unbinned x in [0,5) are assigned 'night'
pos(pos==0) = 5;
bins(pos)'
ans =
'night'
'night'
'morning'
'morning'
'morning'
'morning'
'morning'
'morning'