Plot 3d plot with different colors depending on the X and Y combination

I am plotting a 3D plot, (X,Y,Z).
X and Y are indexes from 1 to 44.
I want that the bar plot color of Z depands on the values of X and Y.
For example:
when X =1 and Y = 20 so the Z bar has to be in color green
when X =10 and Y = 40 so the Z bar has to be in color red
when X =42 and Y = 40 so the Z bar has to be in color blue
The array combination of X and Y are stored in varriable IN, OUT.
for example
IN=[1 5 6 7 8 9 2 10....] (22 values)
OUT=[20 44 21 ....] (22 values)

Answers (2)

Here's the example from that page.
Z = magic(5);
b = bar3(Z);
colorbar
for k = 1:length(b)
zdata = b(k).ZData;
ix=isfinite(zdata(:,1));
zdata(ix,1)=zdata(ix,2);
b(k).CData = zdata;
b(k).FaceColor = 'flat';
end

12 Comments

Thank you for your answer but i want to change the Z bar color according to the coordinates X and Y, as I explain in my question
Now you see how to change the color of individual bars. Replace the values I used in my example to whatever values you want to use.
Indeed I have a matrix of 44x44, so i have many conbination to do.
And I didnt understand well you suggestion
Please share the code you will use to decide the color for an indiviual bar.
here the Array IN and OUT:
IN = [NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 11 12 NaN NaN NaN NaN NaN 18 19 20 21 22 23 24 25 NaN NaN 28 29 30 31 32 33 34 35 36 37 38 39 NaN NaN NaN NaN NaN]
OUT =[1 2 3 4 5 6 7 8 9 10 NaN NaN 13 14 15 16 17 NaN NaN NaN NaN NaN NaN NaN NaN 26 27 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 40 41 42 43 44]
my matrix is 44x44, the size of IN and OUT is 44, where there is NaN means there is no IN or OUT, and the other values are from 1 to 44 which are the indexes.
so you can see I have 3 conbinations (they are :
OUT with IN
OUT with Nan IN /and/ OUT NaN with IN are the same for me
NaN OUT with Nan IN.
so i want to plot the Z bar colors based on this condition.
hope you could understand my issue
So how would you go about coding that up using your logic?
Indeed that my issue to code that
Share what you have tried so far. You might look into if-else or switch statements. See here.
I tried to write this code, but not working for me, i tried many changes no luck
color_matrix = zeros(44, 44);
color_matrix(strcmp(matrix, 'D')) = 1;
color_matrix(strcmp(matrix, 'S')) = 2;
color_matrix(strcmp(matrix, 'DS')) = 3;
color_matrix(cellfun('isempty', matrix)) = 4;
[X, Y] = meshgrid(1:44, 1:44);
bar_colors = [0 0 0; 0 0 1; 0 1 0; 1 1 1];
figure;
ax = gca;
hold(ax, 'on');
for i = 1:numel(X)
x = X(i);
y = Y(i);
z = Z(i); %% here where my data for Z are saved
color_idx = color_matrix(i);
h = bar3(ax, y, z, 1);
zdata = ones(6 * size(z, 2), size(bar_colors, 2));
zdata(:,:) = repmat(bar_colors(color_idx, :), 6 * size(z, 2), 1);
set(h, {'ZData'}, {zdata});
set(h, 'XData', x);
end
hold(ax, 'off');
view(ax, 3);
xlabel(ax, 'X-axis');
ylabel(ax, 'Y-axis');
zlabel(ax, 'Z-axis');
title(ax, '3D Bar Plot');

Sign in to comment.

You're interested in creating a bar chart where you can individually control the color of each bar, correct?
If so, you can start by creating a normal bar chart and collecting the handle, like so:
b = bar(0:5,6:-1:1)
b =
Bar with properties: BarLayout: 'grouped' BarWidth: 0.8000 FaceColor: [0 0.4470 0.7410] EdgeColor: [0 0 0] BaseValue: 0 XData: [0 1 2 3 4 5] YData: [6 5 4 3 2 1] Show all properties
Once you have the handle b, you can set the FaceColor property to 'flat'. This allows you to control the color of each bar individually with the CData property. The CData property has one row per bar, and one column per color.
% Colors are specified as [R G B], where each base color is in the range 0
% to 1.
redColor = [1 0 0];
greenColor = [0 1 0];
blueColor = [0 0 1];
grayColor = [0.5 0.5 0.5];
% You can read the X and Y data of each bar element through XData and
% YData. You can modify the color by changing CData.
% YData is a vector of the same length as your input.
% CData is a matrix with one row per input and one column per color.
b.FaceColor = 'flat';
for idx=1:length(b.XData)
if b.YData(idx) == 1 % Extract elements from YData
b.CData(idx,:) = redColor; % Extract rows from CData
elseif b.YData(idx) == 3
b.CData(idx,:) = greenColor;
elseif b.YData(idx) == 5
b.CData(idx,:) = blueColor;
else
b.CData(idx,:) = grayColor;
end
end

Products

Asked:

on 14 Apr 2023

Edited:

on 5 Jun 2023

Community Treasure Hunt

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

Start Hunting!