필터 지우기
필터 지우기

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