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

5 次查看(过去 30 天)
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!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by