bar chart xtick position

9 次查看(过去 30 天)
GMark
GMark 2013-7-24
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))

回答(2 个)

dpb
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
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
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 个评论
GMark
GMark 2013-7-25
Hi dpb,
thanks for your answers.
The above codes do not work properly for my purpose.
I just solved the problem by using the text function and deleting the xaxis (with a with color). Maybe it is not that elegant, but it works well. Here the codes:
Y= randn(3,2); text1={'text1' 'text2' 'text3'}; h=bar(Y); x=get(h(1),'Xdata'); y=get(h(1),'Ydata'); set(h(1),'FaceColor',[0.4 0.4 0.4]) % dark-grey set(h(2),'FaceColor',[0.8 0.8 0.8]) % light-grey set(gca,'XTick',[]) distance=.5; %distance from xaxis set(gca,'XColor','w') for xc=1:length(x) text(x,y-y-distance,text1,'Rotation',40,'HorizontalAlignment','Right'); end
dpb
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 CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by