How can I draw bar graph from the data in matlab?

4 ビュー (過去 30 日間)
user1234
user1234 2017 年 5 月 7 日
コメント済み: user1234 2017 年 5 月 9 日
I want to draw a bar graph in MATLAB that represent players versus years won. For instance,
_
_____________________________________
Country Years won
______________________________________
US 2002, 2005
Canada 2012, 2013
Belgium 2003, 2004,2011, 2017
Hungary 2001, 2015, 2016
How can I draw the bar of this data values in MATLAB? I was wondering if someone could help me?
  1 件のコメント
user1234
user1234 2017 年 5 月 7 日
編集済み: user1234 2017 年 5 月 7 日
I would like to know the problem of the following codes to generate the bar graphs. Moreover, I need the years to appear on the graph. Anyone help. Thanks.
Names = {'US','Canada','Belgium','Hungary'};
YearsWon = {[2002, 2005],[2012, 2013],...
[2003, 2004,2011, 2017],[2001, 2015, 2016]};
set(gca, 'YTickLabel', Names,...
'XTick',2001:2017);
xlabel('Years Won');ylabel('Country Name')

サインインしてコメントする。

回答 (1 件)

Geoff Hayes
Geoff Hayes 2017 年 5 月 7 日
Hailemariam - this seems like homework, so take a look at the Create Bar Graph with Categorical Data example from MATLAB bar graph. Presumably you will want you x-axis to have labels using each of the four countries from above. This example will show you how to do that.
  5 件のコメント
Geoff Hayes
Geoff Hayes 2017 年 5 月 7 日
But how does the bar "work" for the US as they didn't win in 2003 and 2004? Or are you looking more for a way to fill in the spaces (along the axes) that correspond to wins for the country?
If the latter, then you can try something like
Names = {'US','Canada','Belgium','Hungary'};
YearsWon = {[2002, 2005],[2012, 2013],...
[2003, 2004,2011, 2017],[2001, 2015, 2016]};
set(gca, 'YTick',1:4,'YTickLabel', Names, 'XTick',2001:2017);
ylim(gca,[0 5]);
xlim(gca,[2000 2018]);
hold on;
countryColours = {'r', 'b', 'g', 'm'};
for u=1:length(Names)
countryWins = YearsWon{u};
for v=1:length(countryWins)
winYear = countryWins(v);
fill([winYear winYear+1 winYear+1 winYear], ...
[u u u+1 u+1],countryColours{u});
end
end
axis(gca, 'image');
In the above, we loop through all of the countries and use fill to create a square for the country at the win year (a different colour is used for each country).
user1234
user1234 2017 年 5 月 9 日
Thank you very much!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeJust for fun についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by