如何调整MATLAB中的3-D条组和Y轴标记?
题
我有这样的3D图:
在图的y轴上,每个三个条的每一组都引用相同的参数:x1,x2,x3。我想为每组三个条上的y轴上有一个间距,以便更清楚地知道这些栏是指相同的参数。同时,我想为每组三个条上的Y轴上放一个标签。例如,需要Y轴的以下标签布局:
x1 x2 x3 x1 x2 x3 x1 x2 x3
grid1 grid2 grid3
任何建议都非常欢迎!我用来绘制条形条的代码如下:
Z = rand(9,5);
h = bar3(Z);
[r c] = size(Z);
zdata = [];
for i = 1:c
zdata = [];
for j = 1:r
zdata = [zdata; ones(6,4)*Z(j,i)];
end
set(h(i),'Cdata',zdata)
end
colormap
colorbar
set(gca,'YTickLabel',['x1';'x2';'x3';'x1';'x2';'x3';'x1';'x2';'x3']);
view([-64 44]);
解决方案
您可以通过指定额外的输入到 bar3
指示沿Y轴放置条形柱的位置。您还可以使用该功能在轴上绘制其他文本 text
:
Z = rand(9, 5); % Some random sample data
[r, c] = size(Z); % Size of Z
Y = [1 2 3 5 6 7 9 10 11]; % The positions of bars along the y axis
C = mat2cell(kron(Z, ones(6, 4)), 6*r, 4.*ones(1, c)).'; %' Color data for Z
hBar = bar3(Y, Z); % Create the bar graph
set(hBar, {'CData'}, C); % Add the color data
set(gca, 'YTickLabel', {'x1' 'x2' 'x3'}); % Modify the y axis tick labels
view(-70, 30); % Change the camera view
colorbar; % Add the color bar
text(-2, 2, 'grid1'); % Add "grid1" text
text(-2, 6, 'grid2'); % Add "grid2" text
text(-2, 10, 'grid3'); % Add "grid3" text
请注意,您可能必须调整文本对象的X和Y值,以使它们呈现为您选择的给定相机视图所需的位置。
编辑:
如果您还想在每个条上方显示值,则可以通过将以下内容添加到上述代码中来做到这一点:
hText = text(kron((1:c).', ones(r, 1)), ... %' Column of x values
repmat(Y(:), c, 1), ... % Column of y values
Z(:)+0.05, ... % Column of z values
num2str(Z(:)), ... % Text strings
'HorizontalAlignment', 'center'); % Center the strings
应该指出的是,拥有如此多的绘制文本有些混乱,因为某些文本将重叠或隐藏在酒吧后面。文本也有点多余,因为 颜色 酒吧确实是为了显示价值。
不隶属于 StackOverflow