How to equally stretch horizontal bars?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
Voss
am 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Exploration finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!