changing color and x-axis ticks in barplot

Hi
I'm making a barplot of some tests results, but on the x-axis the standard format uses 1-2-3-4 etc. However, I want to replace these ticks by text.
Furthermore, I want to give the different bars different colors. Is this possible?
Thanks!

回答 (1 件)

Sven
Sven 2011 年 12 月 12 日

2 投票

JW, try this:
h = bar(rand(1,3))
set(gca,'XTickLabel',{'One','Two','Three'})
Setting different colours for each bar is a little tricky because bar() makes the bars as a group (with a consistent colour), but can be done to each bar separately using the faceVertexCData property as follows:
barGrpH = get(h,'Children');
myCols = lines(3);
set(barGrpH ,'FaceVertexCData',myCols)

4 件のコメント

JW
JW 2011 年 12 月 12 日
Thanks!
I used exactly the same, but 'XTick' instead of xticklabel.. stupid mistake..
Do you also know how to give each bar a different color?
Sven
Sven 2011 年 12 月 12 日
Yep, the answer has a second part which sets the colour independently. The command lines(3) makes a set of 3 1-by-3 RGB colours that just happen to be be blue, green, red. You can insert any RGB colours you like here.
JW
JW 2011 年 12 月 13 日
I indeed overlooked the second part of your answer, thanks for your help!
Sven
Sven 2011 年 12 月 13 日
No problems JW... I actually only wrote the first part first, then updated 10 minutes later... a bit sneaky :)
Be sure to hit "accept" if this question's answered - it helps keep lists of answered/unanswered questions organised and should help the next person with your same question find this answer.

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

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

JW
2011 年 12 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by