Change color on a single bar

조회 수: 64 (최근 30일)
Alessandro Ruda
Alessandro Ruda 2021년 3월 9일
댓글: Alessandro Ruda 2021년 3월 10일
Dear MatLab comunity,
I have a problem in changing color on a single or multiple bars in a normal and easy bar graph.
I tried the following:
%% Load Data
RMSD_UDP = load('rmsf_UDP.dat');
% Assign variables
n = RMSD_UDP(:,1); %number of residue
x_UDP = RMSD_UDP(:,2); %RMSF for given residue
%Figure
figure
hold on
title('RMSF UDP');
w1 = 0.5;
b = bar(n,x_UDP,w1)
b.FaceColor = 'g';
b.CData(2,:) = [.5 0 .5];
b.CData(44,:) = ['k'];
xlabel('Residue');
ylabel('RMSF');
xlim([0 364])
ylim([0 5])
It doesn't work but I don't know if it's the MatLab version (R2017a). I tried it on one of the latest ones and it didn't work either. there must be some detail i am not seeing.
Any idea?
Have a nice day,
Alex

채택된 답변

Adam Danz
Adam Danz 2021년 3월 9일
편집: Adam Danz 2021년 3월 9일
  댓글 수: 7
Adam Danz
Adam Danz 2021년 3월 9일
No, that's now how to set CData. You don't lose that opportunity; you never had it in the first place.
The documentation for CData states "By default, when you create a bar chart, the CData property contains a three-column matrix of RGB triplets. You can change the color for a particular bar by changing the corresponding row in the matrix. This property applies only when the FaceColor or EdgeColor property is set to 'flat'."
It won't throw an error because characters are converted to double but the colors will be unrelated to the color characters.
To set the color of several bars,
b.CData(10:20,:) = [1 0 0] .* ones(11,1); % for 11 rows (10:20)
Alessandro Ruda
Alessandro Ruda 2021년 3월 10일
Thank you very much Adam

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by