Generate bar like the attach
Mostrar comentarios más antiguos
Dear friends,
How could I plot bar, for the following data. This graph generated by Excel.
Thank you in advance.

18 comentarios
Chunru
el 15 de Abr. de 2021
Try this:
a = rand(5, 3); % 5 groups of 3 bars
bar(a);
Ameri
el 15 de Abr. de 2021
DGM
el 15 de Abr. de 2021
Depends what you mean by "different nice color". You can specify any color tuple you want, and there are multiple built-in and countless custom colormaps available.
Adam Danz
el 15 de Abr. de 2021
This has been addressed in the forum dozens and dozens of times. Search for methods of changing grouped bar colors and if you have any problems implementing the solution show us what you've got.
If you're asking how to make it look like the excel plot, this is an approximate example.
a = rand(5, 3); % 5 groups of 3 bars
h = bar(a)
cmap=[68 114 196;
165 165 165;
91 155 213]/255;
h(1).FaceColor=cmap(1,:);
h(2).FaceColor=cmap(2,:);
h(3).FaceColor=cmap(3,:);
h(1).EdgeColor=cmap(1,:);
h(2).EdgeColor=cmap(2,:);
h(3).EdgeColor=cmap(3,:);
set(gca,'ygrid','on','xgrid','off')
legend(h,'thing1','thing2','thing3')

Ameri
el 15 de Abr. de 2021
DGM
el 15 de Abr. de 2021
For clarification:
a = rand(5, 3);
is a placeholder. You're supposed to include your data.
Consider the example:
x = [5 10 15 20];
y1 = [11 12 13 14]; % this is one dataset
y2 = [14 15 16 17]; % this is another dataset
y3 = [17 18 19 20]; % and so on
y = cat(1,y1,y2,y3)'; % concatenate them
h = bar(x,y); % plot them
cmap=[68 114 196;
165 165 165;
91 155 213]/255;
h(1).FaceColor=cmap(1,:);
h(2).FaceColor=cmap(2,:);
h(3).FaceColor=cmap(3,:);
h(1).EdgeColor=cmap(1,:);
h(2).EdgeColor=cmap(2,:);
h(3).EdgeColor=cmap(3,:);
set(gca,'ygrid','on','xgrid','off')
legend(h,'thing1','thing2','thing3','location','northwest')
You can either include those vectors as literals or by reading them in from an excel file.
Ameri
el 15 de Abr. de 2021
Ameri
el 15 de Abr. de 2021
DGM
el 15 de Abr. de 2021
You can use the barwidth option
h = bar(x,y,'barwidth',0.6);
Ameri
el 15 de Abr. de 2021
I'm sorry. I didn't see your prior comment. This site tends to be laggy about updating sometimes.
For question 1, that's happening because x has uneven spacing. Try using a categorical array for x:
x = categorical([40 60 80 90 100]);
For question 2, that's a colormap I derived from the image of the excel bar chart you posted. You can use whatever colormap you want. They're just RGB values.
Ameri
el 15 de Abr. de 2021
Ameri
el 16 de Abr. de 2021
Walter Roberson
el 16 de Abr. de 2021
That works.
These days we recommend switching to readtable() instead of xlsread()
Cris LaPierre
el 16 de Abr. de 2021
Respuestas (0)
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



