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

조회 수: 19 (최근 30일)
Tesla
Tesla 2023년 4월 14일
편집: Cris LaPierre 2023년 6월 5일
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)

답변 (2개)

Cris LaPierre
Cris LaPierre 2023년 4월 14일
편집: Cris LaPierre 2023년 4월 14일
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
Tesla
Tesla 2023년 4월 17일
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');

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


Daniel
Daniel 2023년 4월 14일
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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by