Borrar filtros
Borrar filtros

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

5 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Ayush Gupta
Ayush Gupta el 11 de Oct. de 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!

Más respuestas (0)

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by