How to format x date axis?
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
roudan
el 11 de En. de 2018
Comentada: Walter Roberson
el 11 de En. de 2018
Hi
I did try to find a answer online but I didn't get a good one so I am asking here for clearer help.
In my plot (z vs. date) with date(mmm-dd-yyyy) being x axis. I'd like to format x-axis so it start from Jan-01-2008 to Jan-01-2014 with an interval of every 90 days(3 months). I never get correct answers. I am very confusing by the results.
Here are what I did.
1. convert date from excel to datenum, dn_Prod=datenum(CycleDate_Prod,'mm/dd/yyyy');
2. plot the line, hLine=plot(hAx(1),dn_Prod,equivalentheave,'-k','linewidth',2,'DisplayName',legendlabel_equiuplift);
3. format the x-axis
datetick('x','mmm-YYYY','keepticks','keeplimits');
startdatenum=datenum('Jan-01-2008','mmm-dd-yyyy');
enddatenum=datenum('Jan-01-2014','mmm-dd-yyyy');
set(gca,'XTick',[startdatenum:90:enddatenum]);
I think I did correctly, but I never get it right.
Anyone help me find the problem in the above scripts? Thanks a lot.
0 comentarios
Respuesta aceptada
Steven Lord
el 11 de En. de 2018
If you're using a sufficiently recent release of MATLAB, release R2016b or later, you can call plot with a datetime array as one of the inputs. I recommend doing that instead of calling plot with a vector of serial date numbers generated by datenum. For more information see this example in the documentation.
2 comentarios
Walter Roberson
el 11 de En. de 2018
datetick() sets the XTickLabels based upon the current tick positions (if 'keepticks' was specified) or based upon the data range (if 'keepticks' was not specified.) datetick() does not modify the properties of the axes to know that date format should be used for all changes to XTick that are made after datetick() is called.
The R2016b+ approach of using datetime() objects does modify the axes properties to know to continue to use date format when XTick are changed.
Más respuestas (0)
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!