필터 지우기
필터 지우기

Barcharts colours based on other vectors

조회 수: 44 (최근 30일)
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2020년 6월 24일
댓글: Nikolas Spiliopoulos 2020년 6월 25일
Hi all,
I have 3 vectors a,b,c with 4 numbers in each of them
I create another vector d with all the values of vectors a,b,c Sorted.
then I want to plot a barchart of vector d, BUT I would like to have different colours in the bars based on which vector they belong.
Can you help me with this?
for example:
values belong to vector a=green colour
values belong to vector b=blue colour
values belong to vector c=orang colour
Hope the quesiton is clear
thanks in advance
Nikolas

채택된 답변

Adam Danz
Adam Danz 2020년 6월 24일
This has been addressed before within the forum (example1 ,example2).
The basic idea is to store the output handle to the bar objects
bh = bar(____);
Set the FaceColor to flat
bh.FaceColor = 'flat';
Redefine the CDdata which defines the color of each bar
colorData = bh.CData; % Current color arrangement.
% Example: change color of bar #2 to red
bh.CData(2,:) = [1 0 0];
% Example: define the colors based on the jet colormap
bh.CData = jet(size(bh.CData,1));
  댓글 수: 3
Adam Danz
Adam Danz 2020년 6월 25일
You just need to index the color values accordingly.
a = [10 20 30];
b = [15 25 50];
c = [5 40 80];
d=[a b c];
ID = [ones(size(a)), ones(size(b))+1, ones(size(c))+2];
[d_sorted, sortIdx] = sort(d);
ID_sorted = ID(sortIdx);
bh = bar(d_sorted);
colors = jet(max(ID)); % Define a colormap
bh.FaceColor = 'flat';
bh.CData = colors(ID_sorted,:) % set the colors.
Nikolas Spiliopoulos
Nikolas Spiliopoulos 2020년 6월 25일
alright thanks, it works!!!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by