how to set ylim range if two values is less than 0 and the other 4 values is greater than 10 in bar groups figure in matlab

To set the y-axis limit (ylim) range for a bar plot in MATLAB when two values are less than 0 and the other four values are greater than 10, you can use the ylim function in combination with the max and min functions.

First, let's consider an example where you have six values in two groups: group 1 ([-2, 5, 7]) and group 2 ([15, 12, 18]). Here's how you can set the ylim range:

main.m
% Define the values for the groups
group1 = [-2, 5, 7];
group2 = [15, 12, 18];

% Create a bar plot
figure
bar([group1; group2])

% Calculate the minimum and maximum values
minValue = min([group1, group2]);
maxValue = max([group1, group2]);

% Set the ylim range based on the minimum and maximum values
ylim([minValue, maxValue])
330 chars
15 lines

In this example, the minValue will be -2 and the maxValue will be 18. By setting the ylim range using ylim([minValue, maxValue]), the y-axis limit will be adjusted accordingly for your bar plot.

Note that if you have different number and arrangement of values, you will need to adjust the code accordingly. Additionally, you can also add some padding to the ylim range if desired.

related categories

gistlibby LogSnag