Clear Filters
Clear Filters

How to equally stretch horizontal bars?

1 view (last 30 days)
I am using the following matlab code available on mathworks for making horizontal bars. dataStart = [1, 2, 3, 4; 4, 5, 6, 7; 7, 8, 9, 10]; % Start of range for each group dataEnd = [2,3,4,5; 5,6,7,8; 8,9,10,11]; % categories = 1:3; groups = {'group1', 'group2', 'group3', 'group4'}; [numCategories, numGroups] = size(dataStart); h = zeros(1, numGroups); % Plotting ranges for i = 1:numGroups for j = 1:numCategories lineHandle = plot([dataStart(j,i), dataEnd(j,i)], [categories(j), categories(j)], ... 'Color', colors(i,:), 'LineWidth', 15); if j == 1 % Save the handle for the first line of each group for the legend h(i) = lineHandle; end end end the plot generated by above code is also attached. May I request matlab community to suggest me how to equally increase (equal length of each color) and touch the right y-axis. I would appreciate your kind help to suggest me how to do it. Dave

Accepted Answer

Voss
Voss on 9 Apr 2024
Here's your code, with formatting applied and a hold on included and the colors variable defined:
dataStart = [1, 2, 3, 4; 4, 5, 6, 7; 7, 8, 9, 10]; % Start of range for each group
dataEnd = [2,3,4,5; 5,6,7,8; 8,9,10,11];
categories = 1:3;
groups = {'group1', 'group2', 'group3', 'group4'};
colors = [1 0 0; 0 1 0; 0 0 1; 1 1 0];
[numCategories, numGroups] = size(dataStart);
h = zeros(1, numGroups);
% Plotting ranges
figure
hold on
for i = 1:numGroups
for j = 1:numCategories
lineHandle = plot([dataStart(j,i), dataEnd(j,i)], [categories(j), categories(j)], ...
'Color', colors(i,:), 'LineWidth', 15);
if j == 1 % Save the handle for the first line of each group for the legend
h(i) = lineHandle;
end
end
end
To increase the length of the lines so that each category's lines go to 11:
dataStart = [1, 2, 3, 4; 4, 5, 6, 7; 7, 8, 9, 10]; % Start of range for each group
dataEnd = [2,3,4,5; 5,6,7,8; 8,9,10,11];
[numCategories, numGroups] = size(dataStart);
w = (max(dataEnd(:))-dataStart(:,1))/numGroups;
dataStartPlot = dataStart(:,1)+w.*(0:numGroups-1);
dataEndPlot = dataStartPlot+w;
categories = 1:3;
groups = {'group1', 'group2', 'group3', 'group4'};
colors = [1 0 0; 0 1 0; 0 0 1; 1 1 0];
h = zeros(1, numGroups);
% Plotting ranges
figure
hold on
for i = 1:numGroups
for j = 1:numCategories
lineHandle = plot([dataStartPlot(j,i), dataEndPlot(j,i)], [categories(j), categories(j)], ...
'Color', colors(i,:), 'LineWidth', 15);
if j == 1 % Save the handle for the first line of each group for the legend
h(i) = lineHandle;
end
end
end
  2 Comments
Devendra
Devendra on 9 Apr 2024
Edited: Devendra on 9 Apr 2024
Thank you very much for your kind help. I am attaching the figure and request you to kindly have a look on it and suggest me , how to stretch the second and third horizontal bars to start from the april and may months respectively as given in x-axis?
I appreciate your kind cooperation.
Deva
Voss
Voss on 9 Apr 2024
You're welcome!
I guess all you need to do is to change the value of dataStart.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown 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!