How can I plot this figure?

X=[1,2,3,4,5,6,7,8,9,10]
Y=['d=1','d=3','d=2','d=2','d=3','d=1','d=2','d=2','d=1','d=3']
The outcome will be similar to this figure.

4 Commenti

DGM
DGM il 11 Apr 2022
In order to plot some data, you need data. It's not clear that you have any. You have a list of numbers 1-10 and a list of 11 labels (technically, it's only one long conglomeration of labels, because you're concatenating them all into one). It's not enough to just show a picture of an unexplained plot (or part of a plot) and say you want it. Explain what the figure is, what you have, and how they are related.
X=[1,2,3,4,5,6,7,8,9,10]; %days
Y1=[1,0,0,0,1,0,0,0,1,0]; %d=1 (worker 1)
Y2=[0,0,1,1,0,0,1,1,0,0]; %d=2 (worker 2)
Y3=[0,1,0,0,0,1,0,0,0,1]; %d=3 (worker 3)
The problem is as follows:
I have total 10 days, and one of 3 workers can work for a day. For insatnce, Day 1 (i.e., X(1)=1 ), worker 1 (i.e., Y1(1)=1 ) is assinged to perform the task and other workers have day-off (Y2(1)=0 and Y3(1)=0). I know the data, as listed above, but i want to draw a graph that shows the details in a figure.
How are you determining the width of the gray and black strips? Then are you just using repmat() to replicate some row vector vertically to get your vertically striped image?
X=[1,2,3,4,5,6,7,8,9,10]; %days
Y1=[1,0,0,0,1,0,0,0,1,0]; %d=1 (worker 1)
Y2=[0,0,1,1,0,0,1,1,0,0]; %d=2 (worker 2)
Y3=[0,1,0,0,0,1,0,0,0,1]; %d=3 (worker 3)
y2image = uint8(repmat(128*Y2, [15, 1]));
imshow(y2image)
I have used the following codes to plot:
Face(1).Colours=[0 0 0]; Face(2).Colours=[0.8500 0.3250 0.0980]; Face(3).Colours=[0.9290 0.6940 0.1250];
Y1=[1,0,0,0,1,0,0,0,1,0]; %d=1 (worker 1)
Y2=[0,0,1,1,0,0,1,1,0,0]; %d=2 (worker 2)
Y3=[0,1,0,0,0,1,0,0,0,1]; %d=3 (worker 3)
Days=10;
i=1;
while i<=Days
if Y1(i)==1
rectangle('Position',[(i-0.5) 0 1 1],'FaceColor',Face(1).Colours,'EdgeColor',Face(1).Colours,'LineWidth',0.5)
axis([0 Days 0 1])
elseif Y2(i)==1
rectangle('Position',[(i-0.5) 0 1 1],'FaceColor',Face(2).Colours,'EdgeColor',Face(2).Colours,'LineWidth',0.5)
axis([0 Days 0 1])
elseif Y3(i)==1
rectangle('Position',[(i-0.5) 0 1 1],'FaceColor',Face(3).Colours,'EdgeColor',Face(3).Colours,'LineWidth',0.5)
axis([0 Days 0 1])
end
i=i+1;
end
The obtained figure is:
To illsutrate, working days={1, 5, 9} have black bar as worker 1 performed duty in those days. Other days can also be exlained accordingly. Now my question is: (1) how can i insert legend for Y1, Y2 and Y3?; (2) Is there any simple and shortcut way to draw it?
Thanks

Accedi per commentare.

 Risposta accettata

You could use create patch objects instead of rectangle objects, and then make a legend from (some of) those patches.
Or you can create patch objects with NaN data (so they don't appear in the axes) in addition to the rectangle objects you already have, one patch for each color, just to be used for the legend:
Face(1).Colours=[0 0 0]; Face(2).Colours=[0.8500 0.3250 0.0980]; Face(3).Colours=[0.9290 0.6940 0.1250];
Y1=[1,0,0,0,1,0,0,0,1,0]; %d=1 (worker 1)
Y2=[0,0,1,1,0,0,1,1,0,0]; %d=2 (worker 2)
Y3=[0,1,0,0,0,1,0,0,0,1]; %d=3 (worker 3)
Days=10;
i=1;
while i<=Days
if Y1(i)==1
rectangle('Position',[(i-0.5) 0 1 1],'FaceColor',Face(1).Colours,'EdgeColor',Face(1).Colours,'LineWidth',0.5)
% axis([0 Days 0 1]);
elseif Y2(i)==1
rectangle('Position',[(i-0.5) 0 1 1],'FaceColor',Face(2).Colours,'EdgeColor',Face(2).Colours,'LineWidth',0.5)
% axis([0 Days 0 1]);
elseif Y3(i)==1
rectangle('Position',[(i-0.5) 0 1 1],'FaceColor',Face(3).Colours,'EdgeColor',Face(3).Colours,'LineWidth',0.5)
% axis([0 Days 0 1]);
end
i=i+1;
end
% (just set the axes limits once, and include the +/- 0.5)
axis([0.5 Days+0.5 0 1]);
% create three patches with NaN data, to be used for making the legend:
p = [patch(NaN,NaN,Face(1).Colours); patch(NaN,NaN,Face(2).Colours); patch(NaN,NaN,Face(3).Colours)];
% make a legend for those three patches
legend(p,{'Y1' 'Y2' 'Y3'}) % (call them what you like)

Più risposte (0)

Categorie

Richiesto:

SM
il 11 Apr 2022

Risposto:

il 16 Apr 2022

Community Treasure Hunt

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

Start Hunting!

Translated by