필터 지우기
필터 지우기

How to change the color of a specific sub-bar in a 2-D bar graph

조회 수: 4 (최근 30일)
Andrea
Andrea 2014년 7월 7일
댓글: Andrea 2014년 7월 8일
Hi there,
I have a 31x3-matrix where each column represent the number of IP signaled by a security-detection technique day-by-day. I want to plot it in a 2-D bar graph where for each x point, three columns have to be shown (the values of a raw of the matrix above) and the third column have to change color accordingly the values of another 31x1-vector. For example if
data = [4, 2, 2; 9, 5, 6];
difference = [0, 1];
are the matrix of the IP and the vector that determines the color of the third raw, I want the third column of the first day (2) is plotted in a color, let supposed it is green, and the third column of the secondo day (6) is plotted in another, let supposed it is red. I've already done some trials but all failed.
Thank you for your attention.

채택된 답변

Hugo
Hugo 2014년 7월 7일
You can do that by plotting each bar separately. For example, suppose that data is a matrix of 31 x 3 and that the colors for the third columns are in a matrix c of 31 x 1, then you could do
bar((1:31)-.2,data(:,1),.15)
hold on;
bar((1:31),data(:,2),.15)
for ind=1:31,
bar(ind+.2,data(ind,3),.15,'FaceColor',c(ind,:));
end
The first argument in bar is the position of the bars and the .15 there is the width that I chose (you can choose another one of course).
Hope this helps.
  댓글 수: 1
Andrea
Andrea 2014년 7월 8일
When I red your solution I was skeptical about the results, because it talked about plotting each bar separately - but I was wrong ! The final results is exactly what I was locking for !
Thank you Hugo :)

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

추가 답변 (0개)

카테고리

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