フィルターのクリア

How to equally stretch horizontal bars?

2 ビュー (過去 30 日間)
Devendra
Devendra 2024 年 4 月 9 日
コメント済み: Voss 2024 年 4 月 9 日
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 Center および File ExchangeData Exploration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by