Group overlay bar chars using cell with three 10x5 double arrays

I am trying to generate a bar chart using one cell with 3(10*5) random elements as it is illustrated in the ZIMBI.png image
My code is like this:
for i = 1:3
cell_part = abs (randn(10,5))
figure;
bar ( cell_part(1,:))
hold on
% figure;
bar ( cell_part(2,:))
hold on
% figure;
bar ( cell_part(3,:))
hold on
bar ( cell_part(4,:))
hold on
bar ( cell_part(5,:))
cell_total{i}(:,:) = cell_part;
end
figure;
bar(cell_total{1}(:,:)', 'group' )
hold on
bar(cell_total{3}(:,:)', 'group' )
hold on
bar(cell_total{2}(:,:)', 'group' )
hold on
% Unfortunatelly, this code generates the image Cakld.png :
How can I associate the cell position with the bar chart ?

3 comentarios

Scott MacKenzie
Scott MacKenzie el 23 de Jul. de 2021
Editada: Scott MacKenzie el 23 de Jul. de 2021
Seems you are trying to add specific tick labels to the bar chart. However, before you work on that I suggest you rework your code. As is, you are calling the bar function 3x5 = 15 times in the for-loop, then another 3 times after the for-loop. Ouch!
I suggest you first collect together and organize the data you want to plot, then plot the data. The chart you are trying to create only requires one call to the bar function. You'll need to use the option 'stacked'. Then, you can work on the tick labels.
Hi, Scott. Tks for replying to me. Tks for your advice related to the for a loop. I saw this stacked option, but I want overlapped data between the columns of my cell_part(10, 5). It seems that the stacket option put the data over the previous one
I rewrote my code.
for i = 1:3
cell_part = abs (randn(10,5))
cell_total{i}(:,:) = cell_part;
end
figure;
bar(cell_total{1}(:,:)', 'group' )
hold on
bar(cell_total{3}(:,:)', 'group' )
hold on
bar(cell_total{2}(:,:)', 'group' )
hold on
but, still no luck
Scott MacKenzie
Scott MacKenzie el 23 de Jul. de 2021
Editada: Scott MacKenzie el 23 de Jul. de 2021
Still no luck with what? Please try to clarify what you are trying to do. Is the key issue just adding tick labels as in the example chart you posted? If so, just following instructions in @Yazan's answer.

Iniciar sesión para comentar.

 Respuesta aceptada

Yazan
Yazan el 23 de Jul. de 2021
Editada: Yazan el 23 de Jul. de 2021
A very simple solution is to plot the data directly but after introducing matrices of zeros so that the bars look like they are grouped. See the example below.
clear, clc
close all
% toy data of size 10-by-5
data1 = randi(5, [10 5]);
data2 = randi(5, [10 5]);
data3 = randi(5, [10 5]);
% insert zero matrices
data = [data1; zeros(5, 5); data2; zeros(5, 5); data3];
% plot stacked bars normally
bar(data, 'stacked');
ax = gca;
ax.XTick = [5 20 35];
ax.XTickLabel = {'Data1', 'Data2', 'Data3'};

8 comentarios

Hi, Yazan. Tks for replying to me. I saw this stacked option, but I want overlapped data between the columns of my cell_part(10, 5). It seems that the stacket option put the data over the previous one
What do you mean that you need "overlapped data between the columns"?
I added i figure that overlap the data for 1 (10x5) group.
The data in your figure is stacked, as in the example provided by me.
The data is overlayed, not on top of the previous one.
https://www.mathworks.com/help/matlab/creating_plots/overlay-bar-graphs.html
I see now. You can use the following:
clear, clc
close all
% toy data of size 10-by-5
data1 = randi(5, [10 5]);
data2 = randi(5, [10 5]);
data3 = randi(5, [10 5]);
% insert zero matrices
data = [data1; zeros(5, 5); data2; zeros(5, 5); data3];
f = figure; ax = axes(f); hold(ax, 'on')
arrayfun(@(x1) bar(data(:, x1), 'stacked', 'FaceAlpha', 0.3, 'EdgeAlpha', 0.8), 1:5);
ax = gca;
ax.XTick = [5 20 35];
ax.XTickLabel = {'Data1', 'Data2', 'Data3'};
If the colors of the bar are not contrasted enough, then you need to find a set of 5 colors that are suitable and define the colors of the bars when you call the function bar.
aryan ramesh
aryan ramesh el 23 de Jul. de 2021
Editada: aryan ramesh el 23 de Jul. de 2021
Perfect. Tks :)
You're welcome, but don't forget to accept the answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Etiquetas

Preguntada:

el 23 de Jul. de 2021

Comentada:

el 23 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by