How to set up 3D bar grouping and Y axis marking in MATLAB?

I have a three-dimensional graph:

alt text

On the y-axis of the chart, each group of three bars refers to the same parameters: x1, x2, x3. I would like to have a gap along the y axis for each group of three columns, so it becomes more clear that these bands belong 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 scheme for the y axis is desirable:

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

Any suggestions are more than welcome! The code I used to build the columns is below:

 Z = rand(9,5); h = bar3(Z); [rc] = 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]); 
+7
matlab plot label bar-chart
source share
1 answer

You can add bar3 between groups of groups by specifying an additional input bar3 , specifying the positions at which you can place the bar columns along the y axis. You can also build additional text in your axes using the text function:

 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

Please note that you may need to adjust the x and y values โ€‹โ€‹of your text objects so that they appear where you want for your chosen camera view.

EDIT:

If you also want to display values โ€‹โ€‹above each column, you can do this 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 noted that the presence of this multi-structured text becomes a little dirty, as part of the text will overlap or hide behind bars. The text is also a bit redundant, since the color of the bars is really designed to display values.

+10
source share

All Articles