How do I change the colors of Stack Plot bars for different variables

1 回表示 (過去 30 日間)
Faizan Lali
Faizan Lali 2024 年 3 月 26 日
編集済み: Voss 2024 年 3 月 26 日
I want to change the colors of the stack bar individually for 3 different variables. I am following the color map property but it is not working. Here is my code. I want the 3rd variable color should be voilet.
color1 = [0.3, 0.5, 0.7]; % RGB color for Y1
color2 = [0.8, 0.2, 0.2]; % RGB color for Y2
color3 = [0.5, 0.2, 0.9]; % Adjusted RGB color for Y3 (violet)
% Plotting
figure('Position', [100, 100, 800, 600]);
% Define the x-coordinates for each group of bars
x_pos = 1:numel(x_values);
Stack_Parameters = {'AC','JRCP\_Base','Base/Subbase/Subgrade'};
y1_MD = [data(:,1),data(:,3),data(:,5)];
y_sum_MD=sum(y1_MD,2);
y_MD=(y1_MD./y_sum_MD)*100;
y1=y_MD(:,1);
y2=y_MD(:,2);
y3=y_MD(:,3);
bar(x_pos,[y1,y2,y3],'stacked');
% Set custom colors for each bar
colormap([color1; color2; color3]);
% Show all values in the bars
%text(x_pos, y(:,1), compose('%.1f%%', data(:, 1)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
%text(x_pos, percent_group1 + percent_group2, compose('%.1f%%', data(:, 2)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
ylabel('Percentage, %');
% Set y-axis limit to 100%
ylim([0 100]);
xticks(x_pos);
title('FWD 3 Layered Analysis (Modulus)');
xlabel('Station');
legend(Stack_Parameters,'Location', 'best','orientation','horizontal');

採用された回答

Voss
Voss 2024 年 3 月 26 日
編集済み: Voss 2024 年 3 月 26 日
One way is to set the FaceColor directly:
% random data:
x_values = 1:10;
data = rand(10,5);
color1 = [0.3, 0.5, 0.7]; % RGB color for Y1
color2 = [0.8, 0.2, 0.2]; % RGB color for Y2
color3 = [0.5, 0.2, 0.9]; % Adjusted RGB color for Y3 (violet)
% Plotting
figure('Position', [100, 100, 800, 600]);
% Define the x-coordinates for each group of bars
x_pos = 1:numel(x_values);
Stack_Parameters = {'AC','JRCP\_Base','Base/Subbase/Subgrade'};
y1_MD = data(:,[1,3,5]);
y_sum_MD=sum(y1_MD,2);
y_MD=(y1_MD./y_sum_MD)*100;
hb = bar(x_pos,y_MD,'stacked');
% Set custom colors for each bar
set(hb,{'FaceColor'},{color1; color2; color3})
% Show all values in the bars
%text(x_pos, y(:,1), compose('%.1f%%', data(:, 1)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
%text(x_pos, percent_group1 + percent_group2, compose('%.1f%%', data(:, 2)), 'HorizontalAlignment', 'center', 'VerticalAlignment', 'bottom');
ylabel('Percentage, %');
% Set y-axis limit to 100%
ylim([0 100]);
xticks(x_pos);
title('FWD 3 Layered Analysis (Modulus)');
xlabel('Station');
legend(Stack_Parameters,'Location', 'best','orientation','horizontal');
  3 件のコメント
Faizan Lali
Faizan Lali 2024 年 3 月 26 日
Thank you. It helped.
Voss
Voss 2024 年 3 月 26 日
編集済み: Voss 2024 年 3 月 26 日
You're welcome! Any questions, let me know.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by