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

 Akzeptierte Antwort

Adam Danz
Adam Danz am 9 Mär. 2021
Bearbeitet: Adam Danz am 9 Mär. 2021

2 Stimmen

7 Kommentare

Alessandro Ruda
Alessandro Ruda am 9 Mär. 2021
Bearbeitet: Alessandro Ruda am 9 Mär. 2021
It just doesn't work. I tried:
b = bar(n,x_UDP,w1);
b.FaceColor = 'flat';
colorData = b.CData;
b.CData(2,:) = [1 0 0];
I can't make it work!
I got the error:
No appropriate method, property, or field 'CData' for class
'matlab.graphics.chart.primitive.Bar'.
Adam Danz
Adam Danz am 9 Mär. 2021
Bearbeitet: Adam Danz am 9 Mär. 2021
I just noticed you are using r2017a (thank you for including that info). You're right. CData became available starting in r2017b.
You'll either need to upgrade to a recent release of Matlab or perhaps you could plot the bars separately.
Okay I solved it using MatLab R2020b, with the following:
figure
hold on
title('RMSF UDP');
w1 = 0.5;
b = bar(n,x_UDP,w1);
b.FaceColor = 'flat';
colorData = b.CData;
b.CData(2) = ['r'];
b.CData(45:67) = ['r'];
xlabel('Residue');
ylabel('RMSF');
xlim([0 364])
ylim([0 5])
Can I ask you whether I can choose the color of the unselected bars?
If I select flat I usually get a pale blue color and i would like to change it to black. Do I have to define all the other columns?
/alex
Adam Danz
Adam Danz am 9 Mär. 2021
Bearbeitet: Adam Danz am 9 Mär. 2021
Let's say you want the base color to be green and the 2nd bar red,
b.FaceColor = 'flat';
b.CData = [0 1 0] .* ones(size(b.CData,1),1);
b.CData(2,:) = [1 0 0];
Nice! But now I lose the opportunity to select more than one column or a range of columns.
b.CData(45:67) = ['r'];
can that be included in some way?
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 am 10 Mär. 2021
Thank you very much Adam

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by