How to equally stretch horizontal bars?

1 次查看(过去 30 天)
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

采纳的回答

Voss
Voss 2024-4-9
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 个评论
Devendra
Devendra 2024-4-9
编辑:Devendra 2024-4-9
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 2024-4-9
You're welcome!
I guess all you need to do is to change the value of dataStart.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Exploration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by