6 views (last 30 days)

Hello,

I'm using stem plot to show my data here is my code. However, my question is there are spaces between two stem lines, I wanted to fill the space with same stem lines, can someone guide me regarding this. e.g., stem plots 5 and 5 but I really need to insert some more line between these vaules to fill the space and the total array values remain 24 same. Thanks in advace.

t=[5 5 5 5 5 5 6 2 2 2 2 4 4 4 4 4 4 4 1 1 6 6 6 6];

stem(t,'-s','MarkerFaceColor',[0 1 0],'LineWidth',1.5);

hold on

Adam Danz
on 11 Dec 2019

"The Only things that make me in trouble is now the legend problem, I have three data 1, data 2, data 3."

Method 1: turn off IconDisplayStyle

Two steps:

1) turn off the icon display of the polkadot handle

2) call legend after you to step 1.

h = plot(inX,inY,'k.','MarkerSize',4,'MarkerFaceColor','k'); % Plot polkadots

h.Annotation.LegendInformation.IconDisplayStyle = 'off' % Turn off legend display for polkadots

legend(. . .) % Create the legend

Method 2: Specify handles in legend

Two steps:

1) get the handles to all objects you want in the legend

2) Create the legend at the end and provide those handles as input.

hold on

h1 = plot(. . ., 'DisplayName', 'Legend1'); % plot stuff that should appear on legend

h2 = plot(. . ., 'DisplayName', 'Legend2'); % plot more stuff that goes on the legend

h3 = plot(. . . ); % Polkadots

% at the end of the plotting stuff

legend([h1,h2])

Adam Danz
on 12 Dec 2019

Follow these easy steps to get your legend right. If you tried it and the legend still isn't correct, share your updated code and I'll point out which lines are incorrect.

1. For every object on your axes that you want to appear in the legend, add a DisplayName. Here are examples (you can change the display names).

x1=stairs(t,'-s','MarkerFaceColor',[0.9290 0.6940 0.1250],...

'MarkerSize',4,'LineWidth',1.5,'DisplayName', 'Data1');

% ^^^^^^^^^^^^^^^^^^^^^^

viu=fill(polyX,polyY,[1 0 0], 'DisplayName', 'Fill1')

% ^^^^^^^^^^^^^^^^^^^^^

h = plot(inX,inY,'k.','MarkerSize',4,'MarkerFaceColor','k',...

'DisplayName','MyOtherData')

% ^^^^^^^^^^^^^^^^^^^^^^^^^^^

2. If there are any objects that you don't want on the legend apply this to the object handle.

sh2.Annotation.LegendInformation.IconDisplayStyle = 'off'

% ^^^ put object handle here

3. If you've done steps 1 and 2 correctly, when you call legend you just have to use this line without any inputs. This goes at the end of the plotting code.

Load1 = legend(); % no inputs needed!

Adam Danz
on 14 Dec 2019

Thank you, Shane; glad I can help out!

Just FYI, I didn't receive a message, in case it's important.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495986-stem-plot-with-minimal-spaces-between-lines#comment_776728

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495986-stem-plot-with-minimal-spaces-between-lines#comment_776728

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495986-stem-plot-with-minimal-spaces-between-lines#comment_776742

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495986-stem-plot-with-minimal-spaces-between-lines#comment_776742

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495986-stem-plot-with-minimal-spaces-between-lines#comment_776812

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495986-stem-plot-with-minimal-spaces-between-lines#comment_776812

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495986-stem-plot-with-minimal-spaces-between-lines#comment_777006

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/495986-stem-plot-with-minimal-spaces-between-lines#comment_777006

Sign in to comment.