Need a code to create a plot in MATLAB which indicates a BMI chart
10 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
DGM
on 19 Apr 2023
Edited: DGM
on 19 Apr 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 Comments
More Answers (0)
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
