Horizontal bar plot with patch

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)

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

Thank you. Can you please help me how to get this working to the v1 vector in have provided?
What do you want it to look like with the ‘v1’ vector? I have no idea.
ok. for t=1:5, it should be 0 , then t=6:15, it should be 1 and so on. Now there should be one box for each unique value of v1 within the horizontal strip. Say, box 1 for t=1:5, then box 2 for t=6:15. The color intensity should be highest for the max value in v1 (in this case v1=3). In the example i have shown, there are three boxes, for v1 there should be 7, one for each value of v1 in the series.
Try this:
figure
patch([0 1 1 0]*10+5,[0 0 1 1], [0.9 0.1 0.1])
hold on
patch([0 1 1 0]*10+15,[0 0 1 1]*2, [0.9 0.1 0.1])
patch([0 1 1 0]*10+25,[0 0 1 1]*3, [0.6 0.1 0.1])
patch([0 1 1 0]*12+35,[0 0 1 1]*2, [0.5 0.1 0.1])
patch([0 1 1 0]*02+47,[0 0 1 1], [0.4 0.1 0.1])
hold off
Thank you. Can this be done in automated way for any length of vector v1?
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.

Asked:

on 24 Jan 2018

Commented:

on 24 Jan 2018

Community Treasure Hunt

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

Start Hunting!