如何在 Matlab 中控制 bar3 图中组的颜色
How to control colours for group in bar3 plot in Matlab
我在 Matlab 中绘制了 3D 条形图。我希望每个组的颜色相同(例如 s1=blue、s2=green 和 s3=red;见下图)
我使用了以下代码:
Z = data;
Y = [1 300 600]; % The positions of bars along the y axis
h = bar3(Y,Z',0.05);
[nGroup, nBar] = size(Z);
nColors = size(get(gcf, 'colormap'), 1);
colorInd = randi(nColors, nBar, nGroup);
for i = 1:nGroup
c = get(h(i), 'CData');
color = repelem(repmat(colorInd(:, i), 1, 4), 6, 1);
set(h(i), 'CData', color);
end
set(gca, 'YTickLabel',
有什么建议吗?
不是按y轴设置组,而是按x轴设置组,因此每个组都是一个图形对象,并且可以一起着色。
这是一个简单的方法:
Z = randi(7,11,3); % some data with three groups
bar3(Z,0.05); % Each column in Z is one graphic object
set(gca,'XTickLabel',{'s1' 's2' 's3'}); % set the lables of the groups
set(gca,'PlotBoxAspectRatioMode','auto') % make the view wider
set(gca,'YDir','normal') % reverse the y-axis to match your example
colormap(lines(3)) % define 3 colors for coloring by group
view(55,22) % rotate the orintation to match your example
如果您想要问题中的颜色,可以将颜色图替换为:
colormap(flipud(eye(3)))
或输入:
h = bar3(Z,0.05); % instead of bar3(Z,0.05)
set(h,{'FaceColor'},{[0 0 1];[0 1 0];[1 0 0]})
我在 Matlab 中绘制了 3D 条形图。我希望每个组的颜色相同(例如 s1=blue、s2=green 和 s3=red;见下图)
我使用了以下代码:
Z = data;
Y = [1 300 600]; % The positions of bars along the y axis
h = bar3(Y,Z',0.05);
[nGroup, nBar] = size(Z);
nColors = size(get(gcf, 'colormap'), 1);
colorInd = randi(nColors, nBar, nGroup);
for i = 1:nGroup
c = get(h(i), 'CData');
color = repelem(repmat(colorInd(:, i), 1, 4), 6, 1);
set(h(i), 'CData', color);
end
set(gca, 'YTickLabel',
有什么建议吗?
不是按y轴设置组,而是按x轴设置组,因此每个组都是一个图形对象,并且可以一起着色。 这是一个简单的方法:
Z = randi(7,11,3); % some data with three groups
bar3(Z,0.05); % Each column in Z is one graphic object
set(gca,'XTickLabel',{'s1' 's2' 's3'}); % set the lables of the groups
set(gca,'PlotBoxAspectRatioMode','auto') % make the view wider
set(gca,'YDir','normal') % reverse the y-axis to match your example
colormap(lines(3)) % define 3 colors for coloring by group
view(55,22) % rotate the orintation to match your example
如果您想要问题中的颜色,可以将颜色图替换为:
colormap(flipud(eye(3)))
或输入:
h = bar3(Z,0.05); % instead of bar3(Z,0.05)
set(h,{'FaceColor'},{[0 0 1];[0 1 0];[1 0 0]})