Azzera filtri
Azzera filtri

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

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Ayush Gupta
Ayush Gupta il 11 Ott 2023
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!

Più risposte (0)

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by