Horizontal bar plot with patch

3 次查看(过去 30 天)
Sowmya MR
Sowmya MR 2018-1-24
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
. 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)

回答(1 个)

Star Strider
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 个评论
Sowmya MR
Sowmya MR 2018-1-24
Thank you. Can this be done in automated way for any length of vector v1?
Star Strider
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 CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by