bar3 color

21 views (last 30 days)
Emily on 30 Apr 2011
Edited: VBBV on 1 Dec 2021
Hello,
I have been using bar3 to plot a matrix. The matrix is 101x 201. When I use bar3, it plots bars of the same color for the short axis, but changes the color for the long axis. For example, my matrix is A(201,101). Matlab plots A(1,:) of the same color, but instead, I want it to plot A(:,1) of the same color. I have tried using bar3(A') but this does not help. Does anyone have sugestions?
Many thanks

Sarah Wait Zaranek on 2 May 2011
I believe this will work for you. Basically I am resetting each set of bars colormap by changing the CData of the patches that make up the bar. I kept the original plot as a reference in this example.
A = rand(101,201);
figure;
hc = bar3(A);
figure;
h = bar3(A);
for ii = 1:length(h)
zdata = get(h(ii),'ZData');
colorEx = repmat((1:length(zdata))',[1 4]);
set(h(ii),'CData',colorEx)
end
Shaimaa Doma on 11 Mar 2020
If I understand the question correctly, the code works when zData and not colorEx is used as color information:
set(h(ii),'CData',zdata)

VBBV on 1 Dec 2021
Edited: VBBV on 1 Dec 2021
A = rand(10,20);
figure;
hc = bar3(A.'); figure;
h = bar3(A.');
for ii = 1:length(h)
zdata = get(h(ii),'ZData');
colorEx = repmat((1:length(zdata))',[1 4]);
set(h(ii),'CData',zdata);
h(ii).FaceColor = 'interp'; % add this line
end Use FaceColor attribute to the existing bar plot. e.g. shown above