sir i want to set xtick(months) at 0 then how will do this and also want to remove lower,upper horizontal line and right vertical line. how will do this .please help me. my code is this
x = [1:12];
y = [0.2208 -0.2125 0 0.0056 0.0367 2.7150 -3.0450 -10.5039 -7.6274 0 0 0];
figure;
bar(x,y,'k');
%bar(y,'g','EdgeColor',[1,0.5,0.5]);
axis([0 12 -11 11]);
title('Trend of Rainfall by Mann-Kendall for 16 Years','fontSize',16,'fontWeight','bold','FontName','Arial');
months = { 'Jan' 'Feb' 'Mar' 'Apr' 'May' 'Jun' 'Jul' 'Aug' 'Sep' 'Oct' 'Nov' 'Dec'};
xlabel('\fontname{Arial}Months','fontSize',12,'fontWeight','bold','FontName','Arial');
ylabel('\fontname{Arial}Z-Statistics','fontSize',12,'fontWeight','bold','FontName','Arial');
set(gca, 'xTickLabel', months)
thank you in advance

 Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 26 Mai 2014

0 Stimmen

If you want the first month (Jan) to start closer to zero, the placement of the x-axis ticks must be adjusted. The default width of any bar (in your plot) is 0.8 (type help bar in the command window for details on width) and since the mid-point of the bar lines up with ticks, then x has to be adjusted as follows so that the left-edge of the January bar lines up with zero:
halfBarWidth = 0.4;
x = halfBarWidth:1:12 % values = 0.4, 1.4, 2.4,…,11.4
(If you decide to change the bar widths, then the above should be modified accordingly.) Then, just prior to setting the x-tick labels, set the x-tick positions:
set(gca,'xTick',x);
set(gca, 'xTickLabel', months);
The above lines up the first bar with 0. If you want to line up the right-most edge of the December bar, then you will have to adjust your limits to something like:
axis([0 x(end)+halfBarWidth -11 11]);
The second part of your question is to the horizontal and vertical lines. Presumably this means the x-ticks and y-ticks (but not their labels). To do this, just set the tick length to zero:
set(gca,'TickLength',[0 0])
Hope that the above helps!

4 Kommentare

devendra
devendra am 26 Mai 2014
Bearbeitet: devendra am 26 Mai 2014
thank you sir for your answer but this is not working as i want . sir i want to write months (xTicks) next to the 0-axis. In other words, since the Y-axis has both positive and negative values, Matlab automatically shows the xTicks in correspondence of the the last (negative) Y-value. How do I put the xTicks next to the 0-axis. and sir i want to remove whole both upper and lower x-axis horzontal lines and right vertical y-axis lines. like this paper graph
Geoff Hayes
Geoff Hayes am 26 Mai 2014
Bearbeitet: Geoff Hayes am 26 Mai 2014
Okay, an alternative to using the x-tick labels is to just write the months to the plot using the text command. Replace
set(gca,'xTick',x);
set(gca, 'xTickLabel', months);
with
set(gca,'xTick',[]); % no x-ticks will appear
text(x-halfBarWidth/2,zeros(1,12)-1,months); % writes the months in roughly
% the centre of the bar, one
% one unit beneath x-axis
Removing the top, bottom and right vertical bars is a little more tricky. You can move the top and right with just the following
box off;
but you are still stuck with the bottom horizontal line. You can "hide" it by setting the x-color to white
set(gca,'XColor','w');
but then that sets the x-axis label of Months to white too. So, like the link included in the above, you could remove the x-axis label and just use the text command to write Months somewhere in the lower half of the plot. Replace
xlabel('\fontname{Arial}Months','fontSize',12,'fontWeight','bold','FontName','Arial');
with
text(5.5,-10'\fontname{Arial}Months','fontSize',12,'fontWeight','bold','FontName','Arial');
(the above places the text at coordinates (5.5,-10)).
devendra
devendra am 27 Mai 2014
Bearbeitet: devendra am 27 Mai 2014
thank you so much sir this is working properly but one problem is there months is shifted left of the bar i want to this value (months) middle of the bar because bar is representing separate months value.
Just manipulate the x coordinate portion of
text(x-halfBarWidth/2,zeros(1,12)-1,months);
Change the x-halfBarWidth/2 to x and go from there.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by