bar chart xtick position
9 次查看(过去 30 天)
显示 更早的评论
Hi,
I have to create a bar chart with the 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?
these are my codes: bar(data(:,9:10)) legend('Real','Bank','Location','NorthWest') set(gca, 'XTickLabel',text, 'XTick',1:numel(text))
0 个评论
回答(2 个)
dpb
2013-7-24
编辑:dpb
2013-7-24
Unfortunately, the axes object has only two values for the [x|y]AxisLocation property -- Top/Bottom; no Origin choice is available.
Use a second axis whose location is at the origin or draw it manually are only choices. I think the first is doable ok w/ some playing to turn off all the extraneous stuff you won't want; there's an example of using a second axis (altho for different purpose it'll still give you the idea of how to manipulate it) in the section on enhancing graphics.
Well, I tried--I could overlay the second axes object as desired and show the xticks at the y-origin of the first but couldn't ever figure out how to get it to not obscure the bars of the bar plot as long as the 'visible' property is 'on'. Of course, if set it to 'off', then the axis disappears, too. :(
Looks like drawing the line and using text() to label it is probably easiest. Wrap the commands in a utility function to call if doing this often.
I think it's a reasonable enhancement request for the 'Origin' keyword for the axis location.
1 个评论
dpb
2013-7-24
...could overlay the second axes ... and show the xticks at the y-origin of the first but ... it ... obscure[d] the bars of the bar plot...
No time to try right now but I think the way is to draw two--one for <0 and another for >0. Then the xtick can be off on one and on on the other and draw the two plots in their own halves. Set the ylim property of the lower to [-Max 0] and the upper to [0 +Max], of course, and remember to keep the spacing consistent between the two.
A lot of bother, indeed... :( as per usual, write a wrapper.
dpb
2013-7-25
OK, the above will work...
% some sample data...
>> b
b =
-0.4023 0.4081 -0.3920 0.0170
>> b1=b;b1(b1>0)=nan; % separate out +/-
>> b2=b;b2(b2<0)=nan;
>> hax1=axes; % first set of axes
>> pos=get(gca,'position'); % position
>> pos(4)=pos(4)/2; % make half height
>> set(hax1,'position',pos);
>> pos(2)=pos(2)+pos(4); % set bottom at top of first
>> hax2=axes('position',pos);
>> ylim(hax1,[-0.5,0]) % set y axis limits for half range
>> ylim(hax2,[0,0.5])
>> bar(hax1,1:4,b); % do the two plots...
>> bar(hax2,1:4,b2)
>> set(hax1,'xtick',[]) % turn off bottom axis ticks
Salt remainder to suit...
2 个评论
dpb
2013-7-25
That was my alternate suggestion, yes.
What doesn't work properly w/ the two-axis solution, just out of curiosity?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!