フィルターのクリア

How do I change the color of each bar in a plot if I do not have bar groups in MATLAB 7.6 (R2008a) ?

2 ビュー (過去 30 日間)
I use the following code to create a bar plot:
x = [46.6 -34.5 47.9];
bar(x)
I would like each bar to display a different color.

採用された回答

MathWorks Support Team
MathWorks Support Team 2012 年 7 月 11 日
The following is an example of changing color of each bar by changing the color of the patches that compose the bars:
x = [46.6 -34.5 47.9]';
bhandle = bar(x);
ch = get(bhandle,'Children'); %get children of the bar group
fvd = get(ch,'Faces'); %get faces data
fvcd = get(ch,'FaceVertexCData'); %get face vertex cdata
[zs, izs] = sortrows(x,1); %sort the rows ascending by first columns
for i = 1:length(x)
row = izs(i);
fvcd(fvd(row,:)) = i; %adjust the face vertex cdata to be that of the row
end
set(ch,'FaceVertexCData',fvcd) %set to new face vertex cdata
  2 件のコメント
Walter Roberson
Walter Roberson 2015 年 12 月 12 日
Haigen MIN, the above code is not valid for R2014b or later. R2014b and newer do not currently have a way to change the bars individually.
Walter Roberson
Walter Roberson 2015 年 12 月 13 日
Haigen MIN, in R2014b or later, in order to control the color of the bars individually, you will need to draw the bars yourself such as with using fill() or patch()

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

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

タグが未入力です。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by