Horizontal bar plot with patch

2 views (last 30 days)
Sowmya MR
Sowmya MR on 24 Jan 2018
Commented: Star Strider on 24 Jan 2018
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)

Answers (1)

Star Strider
Star Strider on 24 Jan 2018
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 Comments
Sowmya MR
Sowmya MR on 24 Jan 2018
Thank you. Can this be done in automated way for any length of vector v1?
Star Strider
Star Strider on 24 Jan 2018
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!