Datetick does not show enough ticks in the plot

I have a code (below) that contains simple data x, y. My time (x-axis) was initially in numbers from 1-1257, which corresponded to the number of days that I looked at. I converted it to serial dates to build it. However, when it is displayed in MATLAB, there are not enough shortcuts on the x axis. When I try to set more tags, I either return these tags in a sequential format (I want the format "mmm yyyy"), or when I try to use NumTicks, I get a lot of shortcuts, but they are wrong, because they are just the original shortcuts 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 the x axis have more labels in the format "mmm yyyy"?

+4
source share
2 answers

Your code did a great job after it just switched some statements! The trick is to first increase the number of ticks and then call the datetick.

With 30 samples, the labels are, of course, too crowded for any worthy plot. As a solution, I downloaded xticklabelrotate from a file sharing.

Here is the modified code. I used dummy y data so that others can 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 the number of elements over a given period of time is a bad idea, especially when combined with a date format that gives the date very β€œrude”.

Instead, you should put your ticks at the beginning of the month, otherwise a random viewer will be misled.

Instead, I suggest using 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, tags are delayed at 1-month intervals between October 2009 and 54 months later, which runs until June 2013. (Numbers found by trial and error.)

enter image description here

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

+8
source

The quick fix I used for this problem is to increase the width of the graph before using the datetick.

The width of the graph can then be reduced as necessary.

This solution is a bit dodgy fix and not as good as Martin.

 set(gcf,'Position',[xPos yPos width height]); datetick 
0
source

Source: https://habr.com/ru/post/1487894/


All Articles