Custom colors for group of bars in bar plot
2 ビュー (過去 30 日間)
古いコメントを表示
I want to have custom colors for a group of bars in Matlab bar plot.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/147351/image.png)
Like the one showed in the picture, but with different colors. I tried to use the following code but it shows stacked bars instead of the groups.
xdata = [1 2 3];
ydata = [10 20 30; 40 50 60; 5 6 7];
clr = [0 0.8 0;
0.3 0.8 0.8;
0 0 1];
for i = 1:length(xdata)
p = bar(xdata, ydata(i,:));
hold on
set(p,'FaceColor', clr(i,:));
end
Any help is appreciated. Thanks.
0 件のコメント
回答 (1 件)
Ovidiu
2015 年 9 月 30 日
編集済み: Ovidiu
2015 年 9 月 30 日
Here is one solution:
xdata = [1 2 3];
ydata = [10 20 30; 40 50 60; 5 6 7];
clr = [0 0.8 0;
0.3 0.8 0.8;
0 0 1];
colormap(clr);
bar(xdata, ydata);
Notice that if you have a figure with multiple subplots, colormap changes the color scheme for all subplots.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Colormaps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!