如何在 MATLAB 中调整 3D 条形分组和 y 轴标签?

发布于 2024-09-17 05:26:26 字数 644 浏览 14 评论 0原文

我有一个像这样的 3D 图:

alt text

在图的 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]);

I have a 3D plot like this:

alt text

On the y-axis of the plot, each group of three bars refers to the same parameters: x1, x2, x3. I would like to have a spacing on the y-axis for each group of three bars, so that it becomes more clear that those bars are referring to the same parameters. At the same time, I would like to put a label on the y-axis for each group of three bars. For example, the following label layout for the y-axis would be desired:

x1 x2 x3   x1 x2 x3   x1 x2 x3
  grid1     grid2      grid3

Any suggestions are more than welcome! The code that I used to plot the bars is given below:

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]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九八野马 2024-09-24 05:26:32

您可以通过指定 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

应该指出的是,绘制这么多文本会有点麻烦混乱,因为某些文本会重叠或隐藏在栏后面。文本也有点多余,因为条形的颜色实际上是为了显示值。

You can add spacing in between your groups of bars by specifying an additional input to bar3 indicating the positions at which to place the columns of bars along the y axis. You can also plot additional text in your axes using the function 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

enter image description here

Note that you may have to adjust the x and y values of your text objects to get them to render where you want for the given camera view that you choose.

EDIT:

If you'd also like to display the values above each bar, you can do that by adding the following to the above code:

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

It should be pointed out that having this much plotted text gets a little messy, since some of the text will be overlapping or hidden behind the bars. The text is also a bit redundant, since the color of the bars is really meant to show the values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文