Colours of a Histogram

47 ビュー (過去 30 日間)
Brian O'Connell
Brian O'Connell 2022 年 3 月 27 日
コメント済み: Brian O'Connell 2022 年 3 月 27 日
Hi,
I have a histogram with 7 categories, that I created using the code...
data = sum(prod2020)
x = ["Fossil" "Biomass" "Geothermal" "Hydro" "Solar" "Wind" "Other" ];
C = categorical(x);
y = [data];
h = histogram('Categories', C, 'BinCounts', y);
I would like to colour the bars separate colours.
mycolour = ['red' 'cyan' 'black' 'blue' 'yellow' 'green''magenta'];
I don't seem to be able to add this into my command in any way.
any ideas?
thanks
b

採用された回答

Dave B
Dave B 2022 年 3 月 27 日
編集済み: Dave B 2022 年 3 月 27 日
If you're specifying the counts, could you use bar instead of histogram? bar has a CData property for this purpose. The limits will appear slightly different but other than that it's pretty similar.
Notes:
  • I called categorical with (x,x) to ensure the categories are plotted in the same order that they were specified (otherwise bar will place them in alphabetical order).
  • You'll need to specify FaceColor as 'flat' to make bar use CData
  • I used validatecolor to convert the colors to an RGB matrix
I put both into a tiledlayout just to do the comparison side-by-side:
x = ["Fossil" "Biomass" "Geothermal" "Hydro" "Solar" "Wind" "Other" ];
C = categorical(x,x);
y = sum(rand(7));
nexttile;
h = histogram("Categories", C, "BinCounts", y);
nexttile
clrs=validatecolor(["red" "cyan" "black" "blue" "yellow" "green" "magenta"],'multiple');
b = bar(C,y,'FaceColor','flat','CData',clrs,'BarWidth',.9);
  1 件のコメント
Brian O'Connell
Brian O'Connell 2022 年 3 月 27 日
Thank you so much...

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBar Plots についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by