Horizontal bar plot with patch
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi All,
I have the following code. Now if i do plot(t,v1), will get a plot. But what i want is just one horizontal light colored strip and the value of v1 centered within the box. Have attached an example in this email
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/169410/image.jpeg)
. Can someone please help me with this?
v1=[zeros(5,1);ones(10,1);2*ones(10,1);3*ones(10,1);2*ones(12,1);ones(2,1);zeros(5,1)];
t=1:54;
plot(t,v1)
댓글 수: 0
답변 (1개)
Star Strider
2018년 1월 24일
Try this:
figure
patch([0 1 1 0],[0 0 1 1], [0.9 0.1 0.1])
hold on
patch([0 1 1 0]+1,[0 0 1 1], [0.7 0.0 0.1])
patch([0 1 1 0]+2,[0 0 1 1], [0.9 0.1 0.1])
hold off
axis equal
text([0.5 1.5 2.5], [0.5 0.5 0.5], {'0', '20', '0'})
Experiment with the text and RGB colour triplets to get the result you want.
댓글 수: 6
Star Strider
2018년 1월 24일
My pleasure.
You have to redefine ‘v1’ as a matrix of amplitudes and lengths (that I call ‘V1’, then it is straightforward to automate it:
V1 = [0 5; 1 10; 2 10; 3 10; 2 12; 1 2; 0 5]; % V1 = [Amplitude Length] Pairs In Each Row
V1L = [0; cumsum(V1(:,2))]; % Cumulative Lengths
figure
AxH = axes('NextPlot','add');
for k1 = 1:size(V1,1)
patch([0 1 1 0]*V1(k1,2)+V1L(k1),[0 0 1 1]*V1(k1,1), rand(1,3), 'LineWidth',0.1)
end
hold off
This seems to be robust. (I used random colours.)
Experiment to get the result you want.
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!