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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!