Need a code to create a plot in MATLAB which indicates a BMI chart

10 visualizaciones (últimos 30 días)
I want to create a plot as shown below in MATLAB but Iam stuck with the coding part. If you can please help me out, I would appreciate it greatly.
Specifications:
The formula used to calculate the BMI = W / H^2(kg/m^2), where W is the weight in kg and H is the height in meter.
For the four colours curves, use the line width ‘3’. To display a BMI of a person, use the following: circular MarkerSize ‘8’, MarkerEdgeColor ‘black’, andMarkerFaceColor ‘cyan’. To display this you have to get two values from user (height and weight) with the above specifications.
This is the code I have so far which is not exactly producing the expected one:
% Define weight range in kg
weight = 30:1:150;
% Define height range in meters
height = 1.2:0.01:2.2;
% Create meshgrid for weight and height
[Weight, Height] = meshgrid(weight, height);
% Calculate BMI for each combination of weight and height
BMI = Weight ./(Height.^2);
% Define BMI ranges for the different colors
underweight = BMI < 18.5;
normal_weight = BMI >= 18.5 & BMI < 25;
overweight = BMI >= 25 & BMI < 30;
obese = BMI >= 30 & BMI < 40;
extremely_obese = BMI >= 40;
% Create color matrix based on BMI ranges
color = zeros(size(BMI));
color(underweight) = 1; % blue
color(normal_weight) = 2; % green
color(overweight) = 3; % yellow
color(obese) = 4; % orange
color(extremely_obese) = 5; % red
% Plot the BMI chart
figure;
hold on;
plot(Weight(underweight), Height(underweight), 'LineWidth', 3, 'Color', [0 0 1]); % blue
plot(Weight(normal_weight), Height(normal_weight), 'LineWidth', 3, 'Color', [0 1 0]); % green
plot(Weight(overweight), Height(overweight), 'LineWidth', 3, 'Color', [1 1 0]); % yellow
plot(Weight(obese), Height(obese), 'LineWidth', 3, 'Color', [1 0.5 0]); % orange
plot(Weight(extremely_obese), Height(extremely_obese), 'LineWidth', 3, 'Color', [1 0 0]); % red
hold off;
xlabel('Weight (kg)');
ylabel('Height (m)');
title('BMI Chart');
legend('Underweight', 'Normal Weight', 'Overweight', 'Obese', 'Extremely Obese');
  2 comentarios
Dyuman Joshi
Dyuman Joshi el 19 de Abr. de 2023
"I am stuck with the coding part"
Which part?
R Vaidehi
R Vaidehi el 19 de Abr. de 2023
Hi,
The plot Iam able to create is this one which does not exactly match the expected plot: I want the plot to have only four lines or curves. Also, it should have the labels in specific areas of the plot as shown in the expected plot. What can I do to change this plot to match the expected plot?
This is the code I have so far:
% Define weight range in kg
weight = 30:1:150;
% Define height range in meters
height = 1.2:0.01:2.2;
% Create meshgrid for weight and height
[Weight, Height] = meshgrid(weight, height);
% Calculate BMI for each combination of weight and height
BMI = Weight ./(Height.^2);
% Define BMI ranges for the different colors
underweight = BMI < 18.5;
normal_weight = BMI >= 18.5 & BMI < 25;
overweight = BMI >= 25 & BMI < 30;
obese = BMI >= 30 & BMI < 40;
extremely_obese = BMI >= 40;
% Create color matrix based on BMI ranges
color = zeros(size(BMI));
color(underweight) = 1; % blue
color(normal_weight) = 2; % green
color(overweight) = 3; % yellow
color(obese) = 4; % orange
color(extremely_obese) = 5; % red
% Plot the BMI chart
figure;
hold on;
plot(Weight(underweight), Height(underweight), 'LineWidth', 3, 'Color', [0 0 1]); % blue
plot(Weight(normal_weight), Height(normal_weight), 'LineWidth', 3, 'Color', [0 1 0]); % green
plot(Weight(overweight), Height(overweight), 'LineWidth', 3, 'Color', [1 1 0]); % yellow
plot(Weight(obese), Height(obese), 'LineWidth', 3, 'Color', [1 0.5 0]); % orange
plot(Weight(extremely_obese), Height(extremely_obese), 'LineWidth', 3, 'Color', [1 0 0]); % red
hold off;
xlabel('Weight (kg)');
ylabel('Height (m)');
title('BMI Chart');
legend('Underweight', 'Normal Weight', 'Overweight', 'Obese', 'Extremely Obese');

Iniciar sesión para comentar.

Respuesta aceptada

DGM
DGM el 19 de Abr. de 2023
Editada: DGM el 19 de Abr. de 2023
Create a contour plot. You can either use contour() or contourf(), depending on whether you want it filled or not.
% Define weight range in kg
weight = 30:1:150;
% Define height range in meters
height = 1.2:0.01:2.2;
% Create meshgrid for weight and height
[Weight, Height] = meshgrid(weight, height);
% Calculate BMI for each combination of weight and height
BMI = Weight ./(Height.^2);
% create contour plot
breakpts = [0 18.5 25 30 40];
contour(weight,height,BMI,breakpts,'linewidth',3)
% specify colors
myCT = [0 0 1; 0 1 0; 1 1 0; 1 0.5 0; 1 0 0];
colormap(myCT);
% place labels
text(37,2.08,'underweight')
text(78,2,'normal')
% ... and so on
You may choose to rotate the text labels so that they fit better.
  3 comentarios
DGM
DGM el 19 de Abr. de 2023
You can flip the axis direction
set(gca,'ydir','reverse')
R Vaidehi
R Vaidehi el 19 de Abr. de 2023
Thank you so much. I got it now!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Contour Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by