histogram bins with different colors

80 ビュー (過去 30 日間)
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 4 月 26 日
Hello Everybody,
I'd be glad if you could help me in building a histogram with different color for each histogram bin.
Before asking, I've tried to define for loops and a matrix of colors, without succeding.
My problem is that T.ConstructionYear is a 1531 x 1 numeric vector, and I'd have a different color for each bin. I left auto mode for the number of bins choice.
I've also tried to write the function for just two bins.
Thanks in advance for the help.
h1 = histogram(T.ConstructionYear, 'FaceColor', 'b');
xlabel('ConstructionYear');
ylabel('Number of Bridges');
title('Distribution of Construction Year Data');

採用された回答

Scott MacKenzie
Scott MacKenzie 2021 年 4 月 26 日
編集済み: Scott MacKenzie 2021 年 4 月 26 日
I'm not sure if you can do this with histogram. Here's what I put together using histcounts and bar instead:
d1 = rand(1531,1); % put your T.ConstructionYear data here
myColor = rand(10,3); % 10 bins/colors with random r,g,b for each
d2 = histcounts(d1);
b = bar(d2, 'facecolor', 'flat');
b.CData = myColor;
xlabel('ConstructionYear');
ylabel('Number of Bridges');
title('Distribution of Construction Year Data');
  5 件のコメント
Scott MacKenzie
Scott MacKenzie 2021 年 5 月 18 日
Yup. Still getting threads here. Nice work. Congratulations. Arch masonry bridges rule! :)
You've got a lot going on in that chart. Are you using "full screen"? You can do this using
f = gcf;
f.WindowState = 'maximize'; % full screen
You can also play with the size of the tick label fonts using the axis FontSize property. Good luck.
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 5 月 18 日
Thanks Scott!
Wow, with your tip I got a figure that occupies the entire screen.
I didn't know it.
Best!

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

その他の回答 (1 件)

Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 5 月 4 日
編集済み: Giuseppe Degan Di Dieco 2021 年 5 月 4 日
As a follow up to this thread, I've developed the below script to show the legend of multiple bars.
I've found some posts on this wonderful community, and then put them together.
%How to have a legend for each bin. For starting, trial with two bins and
%with a for loop
d1 = rand(2, 1); %a random-value 2 x 1 vector
myColor = rand(2, 3); %2 bins/color triplet with random r, g, b for each
d2 = histcounts(d1); %apply the histcounts function to the data
hold on
for i = 1:length(d1)
b = bar(d1(i), d2);
b.CData(i, :) = myColor(i, :);
end
b.BarWidth = 0.4;
xlabel('Construction Year');
ylabel('Number of Bridges');
title('Distribution of Construction Year Data');
legend('location', 'bestoutside')
hold off

カテゴリ

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

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by