Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datetick not showing enough tick marks in plot

Tags:

plot

matlab

I have a code (below) which plots simple x, y data. My time (x-axis) was originally in numbers from 1-1257 which corresponded to the number of days I was looking at. I converted that to serial dates in order to plot it. However, when it is plotted in MATLAB, there isn't enough labels on the x-axis. When I try to set more tick marks, I either get the labels back in serial format (I want 'mmm yyyy' format), or when I try NumTicks, I get a lot of labels, but they are incorrect since they are just the original labels repeated several times.

% Find indexes at which the lat and lon match the conditions
lon_ind = find(X(:,1) == 224); % Longitude closest to  136 03.56W
lat_ind = find(Y(1,:) == -66.75); % Latitude closest to 66 39.67S

% Pull out all the data at the point 2240W and 66.75
data_point = data_All(lon_ind, lat_ind, :); 
t = 1:1257; % Days 1:1257 inclusive. 20100101 to 20130611
y = reshape(data_point,[],1); % Change data_point into a 1 column matrix 

x = datenum(2009, 12, 31) + t; % Convert t into serial numbers

% str = datestr(x, 'mmm yyyy'); % Choose format for x-axis
plot(x, y); % Plot data

datetick('x','mmm yyyy','keeplimits', 'keepticks'); % Set parameters

% NumTicks = 30;
% L = get(gca,'XLim');
% set(gca,'XTick',linspace(L(1),L(2),NumTicks))

set(gca,'XMinorTick','on','YMinorTick','on'); % Add minor ticks (without labels)

How can I make it so the x-axis has more labels in the 'mmm yyyy' format?

like image 362
SugaKookie Avatar asked Jun 24 '13 13:06

SugaKookie


1 Answers

Your code worked great after only switching some statements around! The trick is to first increase the number of ticks, and then call datetick.

With 30 dateticks, the labels are of course way too crowded for any decent plot. As a solution, I downloaded xticklabelrotate from the file-exchange.

Here is the modified code. I used dummy y-data so that others could play with a workable example.

t = 1:1257; % Days 1:1257 inclusive. 20100101 to 20130611
y = t.^2;
x = datenum(2009, 12, 31) + t;

str = datestr(x, 'mmm yyyy');
plot(x, y);

NumTicks = 30;
L = get(gca,'XLim');
set(gca,'XTick',linspace(L(1),L(2),NumTicks))

datetick('x','mmm yyyy','keeplimits', 'keepticks')
xticklabel_rotate;
set(gca,'XMinorTick','on','YMinorTick','on')

The result looks something like this:

enter image description here

For any real world application, never forget to label the axes! :-)

EDIT

Using the above code to evenly distribute a number of xticks in over a given time-period is a bad idea, especially when combining it with a date-format that only gives the date very "roughly".

Instead, you should place your ticks at the beginning of the months, otherwise the casual viewer will be mislead.

Instead I propose to use this code:

t = 1:1257; % Days 1:1257 inclusive. 20100101 to 20130611
y = t.^2;
x = datenum(2009, 12, 31) + t;

str = datestr(x, 'mmm yyyy');
plot(x, y);

tick_locations = datenum(2009,10:1:54,1);
set(gca,'XTick',tick_locations)

datetick('x','mmm yyyy','keeplimits', 'keepticks')
xticklabel_rotate;

Here, the labels are spaced in 1-month intervals between Oct 2009 until 54 months later, which works out to June 2013. (Numbers found by trial and error.)

enter image description here

The ticks are now always at the beginning of a month, which means the space between them is a bit different from month to month, as it should be. (28 days for FEB-MAR, 31 days for MAR-APR)

like image 133
Martin J.H. Avatar answered Oct 05 '22 13:10

Martin J.H.