How can I get a bar chart with many x inputs in different colours?

1 回表示 (過去 30 日間)
Ludovica Varriale
Ludovica Varriale 2023 年 10 月 11 日
コメント済み: Ludovica Varriale 2023 年 10 月 11 日
Hello everybody,
I am trying to get a bar x,y plot with several bars. I would like to have the bars in different colours using the colorder function (I like the palette "reef" proposed by Matlab itself).
I hope that someone can help :)
Thank you in advance
Here my code:
% Data
x= ["Asp" "Glu" "Ser" "His" "Gly" "Thr" "Arg" "Ala" "Tyr" "Met" "Val" "Phe" "Ile" "Leu" "Lys"];
y= [25.6,40.9,31.7,7.0,39.0,29.0,70.9,4.9,26.9,10.6,34.7,23.9,26.8,47.5,61.7];
figure
hold on
b= bar(x,y);
ylabel('mg/L');
hold off

採用された回答

Ayush Gupta
Ayush Gupta 2023 年 10 月 11 日
You can do something like this.
% Define x and y values for the bars
x = ["Asp" "Glu" "Ser" "His" "Gly" "Thr" "Arg" "Ala" "Tyr" "Met" "Val" "Phe" "Ile" "Leu" "Lys"];
y = [25.6, 40.9, 31.7, 7.0, 39.0, 29.0, 70.9, 4.9, 26.9, 10.6, 34.7, 23.9, 26.8, 47.5, 61.7];
% Define the desired colors for each bar
colors = parula(numel(y));
% Create the bar plot with different colors
b = bar(y);
b.FaceColor = 'flat';
b.CData = colors;
% Set the x-axis tick labels
xticks(1:numel(x));
xticklabels(x);
xtickangle(45);
% Add labels and title
xlabel('XLABEL');
ylabel('YLABEL');
title('Bar Plot');
% Adjust the figure size if needed
fig = gcf;
fig.Position(3:4) = [800 400];
Hope this helps!
  1 件のコメント
Ludovica Varriale
Ludovica Varriale 2023 年 10 月 11 日
Dear Ayush Gupta,
it works perfectly!
Thanks a lot!!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by