How to label Xtick with assigned years?

4 次查看(过去 30 天)
Hello
I have been trying to plot some evolution of parameters as a function of years. Here are my trials with codes. Figure 0 is the default which starts from zero to Nyears. Figure 1 is developed with new vector xdate with each element is integer value of each year in four digits. But I wanted in two digits. After I created the list of strings with two digits, the labels are repeated as in Figure 2. Could you please help me how to fix such issues in Matlab.
X = [0.4851 0.12477 0.079046 0.56795 7.95E-05 0.49443 0.0722 0.87595 ...
0.5065 0.41204 0.69801 0.70715 0.14759 0.54556 0.60394 0.46758 ...
0.30115 0.48743 0.35104 0.0059046 0.21027 0.077238 0.66774 0.28482 ...
0.081285 0.3046 0.32423 0.19326 0 0.34675 0.044938 0.34572 ...
0.68653 0.88543 0.51209 0.15263 0.28867 0.63446 0.027579 0.010771 ...
0.67618 0.75188 0.83475 0.34111 0.43898 0.5252 0.38063 0.50016 ...
0.54488 0.41151 0.31144];
Nyears = 52;
yearStart = 1956;
yearEnd = Nyears + yearStart;
xdate = yearStart+1:yearEnd;
istart = 2;
nend = Nyears;
xdate1 = xdate(istart:nend);
% calls a routine to create a vectors of strings for each years
[ idyears ] = stringYearLabel( yearStart, Nyears );
xtickLbl = [idyears(istart+1); idyears(5:10:end); idyears(end)];
plot(X, 'color', 'k'); %default plot as in Figure 0
plot(xdate1, X, 'color', 'k'); %include time as vector for X values
%set the limit of X axis; by default figure looks like Figure 1
set(gca, 'Xlim', [xdate1(1) xdate(end)]);
% while setting like this plot come out to be terrible (Figure 2), labels limited
set(gca, 'Xticklabel', xtickLbl);
function [ id2digit, idyears ] = stringYearLabel( startYear, Nyears )
% to generate vectors of year labels in string
% id2digit : lists in two digits
% idyears vectors in four digits
idyears = cell(Nyears+1,1);
id2digit = cell(Nyears+1,1);
k = 0;
for i = 1:Nyears+1
idyears{i} = int2str(k + startYear);
id2digit{i} = idyears{i}(end-1:end);
k = k + 1;
end
Thank you for your help...

采纳的回答

dpb
dpb 2016-6-20
Copying your X array, I find
>> Nyr=length(X)
Nyr =
51
instead of 52...minor point perhaps you missed a datapoint in the posting. Anyway, that aside--
yr=1956:1956+Nyr-1; % generate a year vector to match...
figure
plot(yr,X) % plot versus 4-digit year
ylim=[yr(1) yr(end)] % set x-limits to data bounds
set(gca,'fontsize',9) % maybe you like the looks of this "more better"??
For two-digit years,
xt=get(gca,'xtick'); % get the tick values
set(gca,'xticklabel',num2str(mod(xt(:),100),'%02d')) % set mod 100 w/ leading 0
Doing the labels this way makes sure you've got the same number as the number of ticks -- labels and ticks have to be 1:1 or won't use 'em all (if too many) or will repeat (not enough).
NB: The (:) in the reference to xt in the num2str call -- this is a must (or another way to ensure a column vector) as num2str only creates the character string array by row; if the values were in a row vector all the values would come out as one long string. : is the shorthand for "return all elements of the array as a single column".

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by