Bar plot with bars in different colors

조회 수: 7 (최근 30일)
Raffael
Raffael 2013년 1월 16일
편집: Dr. Murtaza Ali Khan 2019년 3월 16일
Hi I have the common problem. I have to plot a bar chart with sorted medians for enzymes and the enzymes shall be colored in blue or red depending on their reversibility.
Here is the data:
data = [.142 31 1;.156 7 1;.191 2 0;.251 6 0]
%First column is the sorted value
%Second column is the index for the YTickLabel
%Third column is the reaction direction
% Data(1,3) = 1 -> bar in red
% Data(1,3) = 0 -> bar in blue
uniNames = {'eno','pck','zwf','...'};
%This was the original script....
h = hist(data(1:end,1))
xlabetxt = uniNames(data(:,2));
ylim([0 .5])
text(1:length(xlabetxt),repmat(-max(ylim)/50,length(xlabetxt),1),xlabetxt','horizontalalignment','right','Rotation',90,'FontSize',15)
text
text(.55,77.5,'A','FontSize',15)
ylabel('median log2 fold change','FontSize',15)

채택된 답변

Thorsten
Thorsten 2013년 1월 16일
data = [.142 3 1;.156 5 1;.191 2 0;.251 4 0];
%First column is the sorted value
%Second column is the index for the YTickLabel
%Third column is the reaction direction
% Data(1,3) = 1 -> bar in red
% Data(1,3) = 0 -> bar in blue
uniNames = {'eno','pck','zwf','foo' 'bar'};
%This was the original script....
H = data(:, 1);
N = numel(H);
for i=1:N
h = bar(i, H(i));
if i == 1, hold on, end
if data(i, 3) == 1
col = 'r';
else
col = 'b';
end
set(h, 'FaceColor', col)
end
set(gca, 'XTickLabel', '')
xlabetxt = uniNames(data(:,2));
ylim([0 .5]); ypos = -max(ylim)/50;
text(1:N,repmat(ypos,N,1), ...
xlabetxt','horizontalalignment','right','Rotation',90,'FontSize',15)
text(.55,77.5,'A','FontSize',15)
ylabel('median log2 fold change','FontSize',15)

추가 답변 (5개)

Hello kity
Hello kity 2013년 1월 18일
편집: Hello kity 2013년 1월 18일
you can also manually select bar and color:
bar(1, Data, 'colorcode')
hold on
bar(2, Data, 'colorcode') , bar (3, Data, 'colorcode')
that is what Thorsten does here:
h = bar(i, H(i));

Luke
Luke 2017년 7월 6일
If you want all your data to be displayed in different colors, an easy way to do this is:
bar(diag(data_vector),'stacked')
  댓글 수: 1
Zara Khan
Zara Khan 2018년 12월 27일
This is the most easiest way that we can do. Thanks a ton.

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


Dr. Murtaza Ali Khan
Dr. Murtaza Ali Khan 2019년 3월 16일
편집: Dr. Murtaza Ali Khan 2019년 3월 16일
mydata=rand(1,10)
color= ['r','g','b','k'];
figure, hold on
% % if data is more than colors then colors will be repeated
m = length(color);
for k = 1:length(mydata)
i = mod(k-1,m); %%i is remainder after division of k-1 by m
i = i+1;
h=bar(k,mydata(k));
set(h,'FaceColor',color(i));
end

Raffael
Raffael 2013년 1월 17일
Exactly what I looked for.
Thank you so much you made my day...
  댓글 수: 1
Thorsten
Thorsten 2013년 1월 18일
Cool. My pleasure. Please just accept my answer to close the case :-)

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


SUDHEEP
SUDHEEP 2013년 10월 30일
try this > a=2; >> b=4; >> bar(a) >> hold on >> bar(2,b,'r') >> axis([0 5 0 6])

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by