how to write years in xtick.

sir i want to write years in xtick like this figure and also write R2 value in figure how can do this? please help.thank you in advance.

답변 (2개)

Chad Greene
Chad Greene 2014년 6월 18일

2 개 추천

If your x vector is in datenum format, you can use datetick('x','yyyy').
Azzi Abdelmalek
Azzi Abdelmalek 2014년 5월 26일

1 개 추천

댓글 수: 4

ravi
ravi 2014년 5월 27일
Thank you sir for giving answer but in this example shows months in xtick how can put here years.please help me sir.
ravi
ravi 2014년 6월 18일
sir i am trying this example but i am unable to this please help me how will set years in xtick like attached figure
I adapted the code from the previous link to plot x,y
% Generate some test data. Assume that the X-axis represents months.
x = 1901:3:2003
y = 10*rand(1,length(x));
% Plot the data.
h = plot(x,y);
% Reduce the size of the axis so that all the labels fit in the figure.
pos = get(gca,'Position');
set(gca,'Position',[pos(1), .2, pos(3) .65])
% Add a title.
title('This is a title')
% Set the X-Tick locations so that every other month is labeled.
Xl = [1901 2003];
set(gca,'XTick',x,'XLim',Xl);
% Add the months as tick labels.
years=cellfun(@num2str,num2cell(x),'un',0)
ax = axis; % Current axis limits
axis(axis); % Set the axis limit modes (e.g. XLimMode) to manual
Yl = ax(3:4); % Y-axis limits
% Place the teaxt labels
t = text(x,Yl(1)*ones(1,length(x)),years);
set(t,'HorizontalAlignment','right','VerticalAlignment','top', ...
'Rotation',90);
% Remove the default labels
set(gca,'XTickLabel','')
% Get the Extent of each text object. This
% loop is unavoidable.
for i = 1:length(t)
ext(i,:) = get(t(i),'Extent');
end
% Determine the lowest point. The X-label will be
% placed so that the top is aligned with this point.
LowYPoint = min(ext(:,2));
% Place the axis label at this point
XMidPoint = Xl(1)+abs(diff(Xl))/2;
tl = text(XMidPoint,LowYPoint,'X-Axis Label', ...
'VerticalAlignment','top', ...
'HorizontalAlignment','center');
ravi
ravi 2014년 6월 19일
sir when i am adding your code in my code then years are perfectly plot in the figure but timeseries is not ploting(showing blank) because yaxis limit set 0 to 1.how can remove this fault from code
subplot(2,2,2);
yrs = linspace(1,86,86);
rainfall = y2;
b = polyfit(yrs,rainfall, 1);
fr = polyval(b, yrs);
plot(yrs, rainfall, '-b')
hold on
plot(yrs, fr, '-r')
hold off
h1 = legend('Data', 'Linear', 'Location','NE');
set(h1, 'FontSize',8)
[xlim ylim]
textposx = diff(xlim)*0.50+min(xlim);
textposy = diff(ylim)*0.95+min(ylim);
text(textposx,textposy, sprintf('y = %.2f*x %c %0.2f', b(1),
char(45-(sign(b(1))+1)), abs(b(2))), 'FontSize',8)
title(' CCSM4(rcp2.6) : hr')
xl = get(gca,'xlim')';
xt=linspace(xl(1),xl(2),2100-2015+1);
set(gca,'xtick',xt)
set(gca,'xticklabel',cellfun(@num2str,num2cell(2015:2100),'un',0))
xlabel('\fontname{Arial}Year','fontSize',10,'fontWeight','bold','FontName','Arial');
ylabel('\fontname{Arial}Rainfall(mm)','fontSize',10,'fontWeight','bold','FontName','Arial');
%text(0.6,0.85,'R2=-0.2580')
% Set the X-Tick locations so that every other month is labeled.
x = 2015:5:2100
Xl = [2015 2100];
set(gca,'XTick',x,'XLim',Xl);
% Add the months as tick labels.
years=cellfun(@num2str,num2cell(x),'un',0)
ax = axis; % Current axis limits
axis(axis); % Set the axis limit modes (e.g. XLimMode) to manual
Yl = ax(3:4); % Y-axis limits
% Place the teaxt labels
t = text(x,Yl(1)*ones(1,length(x)),years);
set(t,'HorizontalAlignment','right','VerticalAlignment','top', ... 'Rotation',90);
% Remove the default labels
set(gca,'XTickLabel','')
% Get the Extent of each text object. This
% loop is unavoidable.
for i = 1:length(t)
ext(i,:) = get(t(i),'Extent');
end
% Determine the lowest point. The X-label will be
% placed so that the top is aligned with this point.
LowYPoint = min(ext(:,2));
% Place the axis label at this point
XMidPoint = Xl(1)+abs(diff(Xl))/2;
tl = text(XMidPoint,LowYPoint,'Year','fontSize',10,'fontWeight','bold','FontName','Arial','VerticalAlignment','top','HorizontalAlignment','center');

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Axis Labels에 대해 자세히 알아보기

질문:

2014년 5월 26일

댓글:

2014년 6월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by