Stem plot with minimal spaces between lines

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

4 Comments

Hmmm, it sounds like you want to use fill to create a solid patch defined by your (x,y) coordinates. How does your goal differ from that?
@Adam Danz, I was looking to refer you to this question but you got it ;) My goal is the same, but I was looking to do it in another way. Can you answer the last part of this question
Done. Let me know if there are any other loose ends.
@Adam, Fantastic, you're great. The Only things that make me in trouble is now the legend problem, I have three data 1, data 2, data 3. 'data 1' is simple plot, 'data 2' is filled with color and polkadots and '3' is filled with color only. So the legend should show this way, but in my case legend show the data but in data 2 only shows symbol and in data 3 shows the color of data 2. Kindly see image it illustrates the problem.Legend.PNG

Sign in to comment.

 Accepted Answer

"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])

8 Comments

Thanks for answer, however, I got error for the second method and the first methond doesnot make any changes therefore for the sake for understanding here is my code. Just for the consideration I would say that for "data 2" I wanted to show in the legend the filled colour as well as polkadots. Sorry for taking your time
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];
t1=[1 2 3 4 5 9 5 6 7 8 39 42 31 2 3 4 5 6 7 8 9 22 23 24];
t2=[11 12 13 14 15 19 15 16 17 8 3 4 3 2 13 14 15 16 17 8 9 22 23 24];
x1=stairs(t,'-s','MarkerFaceColor',[0.9290 0.6940 0.1250],'MarkerSize',4,'LineWidth',1.5);
hold on
sh2=stairs(1:24,t1, 'o','color',[1 0 0],'MarkerEdgeColor','r', 'MarkerSize',4,'LineWidth',1.3);
x = [sh2.XData(1),repelem(sh2.XData(2:end),2)];
y = [repelem(sh2.YData(1:end-1),2),sh2.YData(end)];
polyX = [x,fliplr(x)];
polyY = [y,bottom*ones(size(y))];
viu=fill(polyX,polyY,[1 0 0])
viu.FaceAlpha = 0.2;
[dotsX,dotsY] = ndgrid(...
linspace(min(polyX), max(polyX), 50), ...
linspace(min(polyY), max(polyY), 50));
[in,on] = inpolygon(dotsX,dotsY,polyX,polyY);
inX = dotsX(in|on);
inY = dotsY(in|on);
h = plot(inX,inY,'k.','MarkerSize',4,'MarkerFaceColor','k');
hold on
sh=stairs(1:24,t2,'-d','color',[1 1 0],'LineWidth',1);
bottom = 0;
x = [sh.XData(1),repelem(sh.XData(2:end),2)];
y = [repelem(sh.YData(1:end-1),2),sh.YData(end)];
h=fill([x,fliplr(x)],[y,bottom*ones(size(y))],[1 1 0]);
h.FaceAlpha = 0.4;
hold on
xlabel('x-axis');
ylabel('y-axis');
yyaxis right
ylabel('Related funciton');
xlim([1 20]);
ylim([0 20]);
hold on
plt = gca;
plt.YAxis(2).Color = 'k';
set(gca, 'XTick',1:24, 'XTickLabel',{'1' '' '' '' '7' '' '' '' '8' '' '' '' '1' '' '' '' '7' '' '' '' '9' '' '' '1'});
title('Each Daybase information');
hold on
h1 = plot(x1, 'data 1', 'Legend1');
h2 = plot(sh1, 'data 2', 'Legend2');
h3 = plot(sh2, 'data 3', 'Legend1');
% at the end of the plotting stuff
legend([h1,h2, h3])
Load1=legend('Data1','Data2 ','Data','FontWeight', 'bold');
Load1.FontSize = 9;
grid on
" I got error for the second method"
What's the full error message?
" and the first methond doesnot make any changes"
If implemented properly, it will prevent the 2nd line object from appearing on the axes. Here's a demo.
clf()
plot(rand(1,5),'bo','DisplayName','BlueDots')
hold on
h = plot(3, .5, 'm*','MarkerSize',20);
h.Annotation.LegendInformation.IconDisplayStyle = 'off' % Turn off legend display for polkadots
legend()
"Just for the consideration I would say that for "data 2" I wanted to show in the legend the filled colour as well as polkadots"
Ah, I see what you mean. You want the filled object to appear in the legend and it should appear with polkadots embedded. There's no simply way to do that. The filled color and the polkadots are two different objects and cannot be easily combined in the legend. Check out this review of a file exchange function capable of doing this: https://blogs.mathworks.com/pick/2011/07/15/creating-hatched-patches/
@Adam, thanks for the anwer. The error I got "", however, I you can make correction according to second method as you see I already did that (in the above code) but it didn't work for me. While about the last part of your answer, I'm trying to do in two ways either filled color appeared with markerstyle or polkadots and as you said its not trivial but what you think about if legend appear with filled color and marker style (Not polkadots)?
"The error I got "", "
Huh? I think the copy-paste didn't work.
Are you talking about the error produced by these lines?
h1 = plot(x1, 'data 1', 'Legend1');
h2 = plot(sh1, 'data 2', 'Legend2');
h3 = plot(sh2, 'data 3', 'Legend1');
That's not valid syntax. What's "data 1" and "Legend1" etc? Are you aiming for something like this line below that specifies the legend display name?
h1 = plot(x1, 'DisplayName', 'Legend1');
While about the last part of your answer, what you think about if legend appear with filled color and marker style (Not polkadots)?
What we are calling "polkadots" are really just marker style '.' repeated at regular intervals across the polygon. And there's no easy way to add a marker type to the fill object within the legend. The functions I mentioned in the file exchange does that, though.
The error was "Not enough input arguments" and Yes I was aiming as you mentioned but couldn't get it. Now as you mentioned (Lastly) and I'm trying two things
(1) that in legend only filled color appear for data 2 and data 3 case.
(2) worest case only line with marker style appears
I used your first method to skip polkadot, however, I don't get this for second data which shows line with marzker style instead of filled color, can you put light on that, I wil add code again
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];
t1=[1 2 3 4 5 9 5 6 7 8 39 42 31 2 3 4 5 6 7 8 9 22 23 24];
t2=[11 12 13 14 15 19 15 16 17 8 3 4 3 2 13 14 15 16 17 8 9 22 23 24];
x1=stairs(t,'-s','MarkerFaceColor',[0.9290 0.6940 0.1250],'MarkerSize',4,'LineWidth',1.5);
hold on
sh2=stairs(1:24,t1, 'o','color',[1 0 0],'MarkerEdgeColor','r', 'MarkerSize',4,'LineWidth',1.3);
sh2.Annotation.LegendInformation.IconDisplayStyle = 'off' % Turn off legend display for polkadots
% The step you mentioned%
x = [sh2.XData(1),repelem(sh2.XData(2:end),2)];
y = [repelem(sh2.YData(1:end-1),2),sh2.YData(end)];
polyX = [x,fliplr(x)];
polyY = [y,bottom*ones(size(y))];
viu=fill(polyX,polyY,[1 0 0])
viu.FaceAlpha = 0.2;
[dotsX,dotsY] = ndgrid(...
linspace(min(polyX), max(polyX), 50), ...
linspace(min(polyY), max(polyY), 50));
[in,on] = inpolygon(dotsX,dotsY,polyX,polyY);
inX = dotsX(in|on);
inY = dotsY(in|on);
h = plot(inX,inY,'k.','MarkerSize',4,'MarkerFaceColor','k');
hold on
sh=stairs(1:24,t2,'-d','color',[1 1 0],'LineWidth',1);
bottom = 0;
x = [sh.XData(1),repelem(sh.XData(2:end),2)];
y = [repelem(sh.YData(1:end-1),2),sh.YData(end)];
h=fill([x,fliplr(x)],[y,bottom*ones(size(y))],[1 1 0]);
h.FaceAlpha = 0.4;
hold on
xlabel('x-axis');
ylabel('y-axis');
yyaxis right
ylabel('Related funciton');
xlim([1 20]);
ylim([0 20]);
hold on
plt = gca;
plt.YAxis(2).Color = 'k';
set(gca, 'XTick',1:24, 'XTickLabel',{'1' '' '' '' '7' '' '' '' '8' '' '' '' '1' '' '' '' '7' '' '' '' '9' '' '' '1'});
title('Each Daybase information');
hold o
Load1=legend('Data1','Data2 ','Data','FontWeight', 'bold');
Load1.FontSize = 9;
grid on
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!
You are real mentor @Adam Danz, I will keep you in my loop for the help. (I sent you a message can you respond to that).
Thank you
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.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Asked:

on 10 Dec 2019

Commented:

on 14 Dec 2019

Community Treasure Hunt

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

Start Hunting!