Two labels for xaxis for bar graph

12 ビュー (過去 30 日間)
Kamran Mukhtar
Kamran Mukhtar 2020 年 4 月 7 日
コメント済み: Kamran Mukhtar 2020 年 4 月 9 日
Hi
I have array of some positive and negative values and draw bar graph. I want to label top and bottom of x-axis based upon positive and negative values.
  3 件のコメント
Kamran Mukhtar
Kamran Mukhtar 2020 年 4 月 8 日
ap=find(a>=0);
apv=a(ap);
namep=name(ap);
an=find(a<0);
anv=a(an);
namen=name(an);
figure(1)
bar(ap,apv)
set(gca,'xtick',ap,'xticklabel',name(ap),'fontsize',8);xtickangle(90)
set(gca,'xaxisLocation','top')
hold on
bar(an,anv)
set(gca,'xtick',an,'xticklabel',name(an),'fontsize',8);xtickangle(90)
set(gca,'xaxisLocation','bottom')
Where a is array consisting of positive and negative numbers and names is corresponding xlabels
dpb
dpb 2020 年 4 月 8 日
編集済み: dpb 2020 年 4 月 8 日
...
bar(ap,apv)
set(gca,'xtick',ap,'xticklabel',name(ap),'fontsize',8);xtickangle(90)
set(gca,'xaxisLocation','top')
hold on
bar(an,anv)
set(gca,'xtick',an,'xticklabel',name(an),'fontsize',8);xtickangle(90)
set(gca,'xaxisLocation','bottom')
You have only one axis here so you can't have two 'XAxisLocation' positions...you can switch the location back and forth from top to bottom as you've done here, but you can't have two...

サインインしてコメントする。

採用された回答

dpb
dpb 2020 年 4 月 8 日
編集済み: dpb 2020 年 4 月 8 日
Per above, you must have a second axes. And, you can do it with only one bar plot instead of two...
ip=(a>=0); % logical vector
in=find(~ip); % convert to position array for negative
ip=find(ip); % ditto now for negative
figure(1)
hBar=bar(a); % draw bargraph, all data first..
hAx=gca; % get axes handle
hAx(2)=axes('Position',hAx.Position,'XAxisLocation','top','Color','none'); % make the second axes
hAx(2).Xlim=hAx(1).Xlim; % make limits match
hAx(2).Ylim=hAx(1).Ylim; hAx(2).YTick=''; % and hide y axis labels
set(hAx,'Box','off'); % so ticks don't show on opposite axis
% now fixup the bar plot to look as desired...
hBar.Color='flat'; % so can use CData array for colors by bar
hBar.CData(in,:)=repmat([1 0 0],numel(in),1); % negative bars red
set(hAx(1),'xtick',in,'xticklabel',name(in),'fontsize',8);xtickangle(hAx(1),90)
set(hAx(2),'xtick',ip,'xticklabel',name(ip),'fontsize',8);xtickangle(hAx(1),90)
Caveat: Air code; I did a sample with made up data here to get the desired effect and then edited on your code to try to match...may be a few typos or oversights but idea will work...the figure I had here that used data from another earlier Answers Q? that was counting letters in a string <Answers/515440-how-can-i-create-a-array-of-letters-from-a-sentence> for the data (the data plotted here are the counts minus mean counts excluding the blanks):
  1 件のコメント
Kamran Mukhtar
Kamran Mukhtar 2020 年 4 月 9 日
You are great...Thanks

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by