You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Generate bar like the attach
2 views (last 30 days)
Show older comments
Dear friends,
How could I plot bar, for the following data. This graph generated by Excel.
Thank you in advance.
18 Comments
DGM
on 15 Apr 2021
Edited: DGM
on 15 Apr 2021
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
on 15 Apr 2021
Thank you alot of, there are several issue please
1- x (number of nodes must be 40 60 80 90 100) not from 1 to 5
2- y (pdr ratio) classified as (10 20 30 40 50 60 70 80 90 100)
3- each bar must refers to his data from excel
I am very grateful for your help
DGM
on 15 Apr 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
on 15 Apr 2021
1- Here a gap or space between some bars is large, where the space between (80 - 90 ) is small, while the space between (40-60) is large. how could make this space are equlals between groups please.
2-and what about this please ? how you selected this number and why
cmap=[68 114 196;
165 165 165;
91 155 213]/255;
DGM
on 15 Apr 2021
Edited: DGM
on 15 Apr 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.
Walter Roberson
on 16 Apr 2021
That works.
These days we recommend switching to readtable() instead of xlsread()
Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)