Bar plot with lower values not fixed

조회 수: 1 (최근 30일)
Todd Welti
Todd Welti 2023년 9월 25일
댓글: Star Strider 2023년 9월 26일
I'd like to make a plot for 9 item, each having 2 bars spaced close together, showing two related values. I want to have the bottoms of each bar not fixed at 0 or at some fixed offset. i want to show the range of values for each object in other words, max and min. no outliers or whiskers or percentiles. Kind of like as shown in the picture, but with EACH of the bottoms of the bars specifiable in Y value, not fixed..

채택된 답변

Star Strider
Star Strider 2023년 9월 25일
Use the patch function in a loop —
x = (1 : 9).'; % Assume Column Vectors
xd = repmat([-0.1 0.1], numel(x), 1) + x
xd = 9×2
0.9000 1.1000 1.9000 2.1000 2.9000 3.1000 3.9000 4.1000 4.9000 5.1000 5.9000 6.1000 6.9000 7.1000 7.9000 8.1000 8.9000 9.1000
xos = 0.125; % Spatial Separation ('x')
y = rand(numel(x),2) % Y-Values
y = 9×2
0.4302 0.6695 0.0597 0.3789 0.8846 0.3307 0.5977 0.8536 0.3026 0.2960 0.9167 0.9977 0.7873 0.5228 0.9008 0.2920 0.9802 0.0182
VarNames = ["A","B","C","D","E","F","G","H","I"];
figure
hold on
for k = 1:numel(x)
patch([xd(k,:) flip(xd(k,:))]-xos, [-1 -1 1 1]*y(k,1)/2, 'r')
patch([xd(k,:) flip(xd(k,:))]+xos, [-1 -1 1 1]*y(k,2)/2, 'b')
end
hold off
xticks(x)
xticklabels(VarNames)
This sets the bars to be symmetric about the midpoiint. It should not be difficult to adjust the code to use other options, since that simply requires changing the y-argument to the patch calls.
.
  댓글 수: 4
Todd Welti
Todd Welti 2023년 9월 26일
Yes, i suppose the patch would be more flexible.
Star Strider
Star Strider 2023년 9월 26일
It has its advantages, certainly.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 9월 25일
This exercise can be done using bar(), xticks, xticklabels, xtickangle commands:
G = randi([0, 15], 9,2);
Names = {'A(x)'; 'AB(y)'; 'ABC(y)'; 'ABCD(x1)'; 'ABBA(y1)'; 'BAC(z1)'; 'DABA(x,y)'; 'DABC1(w)'; 'ABCD13(u)'};
bar(G), grid on
xticks(1:9)
xticklabels(Names)
xtickangle(45)
ylim([0, 16])
legend('ver: One', 'ver: Two')
  댓글 수: 1
Todd Welti
Todd Welti 2023년 9월 26일
Yeah, that is what I already had. The idea was to be able to also specify the minimum values, not all at 0.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by