Two Stem Plots with Different Base Values on the Same Graph

47 views (last 30 days)
Chien-Cheng Chiu
Chien-Cheng Chiu on 18 Sep 2020
Edited: VBBV on 14 Nov 2020
I want to plot two stem plots on the same graph so that "stem1" has a base value at 0 and "stem2" has a base value at -y_shift = 1. However, when I run the code, "stem1" would start on a base value at 0 for some time before its base value at 0 changes to the same base value as "stem2" and stays at the base value of -1. Is there any way to prevent that from happening?
for k = 1:10:m % for faster animations i.e. skip frame
z0 = u_approx_dirac(k,:);
z1 = dirac(k,:);
clf
hold on
plot(x, zeta(x), 'r');
plot(x_dirac , z0 , 'k');
stem1 = stem(x (z1 ~= 0), z1 (z1 ~= 0), 'filled', 'k');
set(stem1, 'LineWidth', 2, 'BaseValue', 0, 'ShowBaseLine', 'off');
time = t(1:2:k);
ref = reflectedwave(y(1:2:k));
dirac_ref = dirac_reflectedwave(y(2:2:k));
plot(time, ref - y_shift, 'b');
stem2 = stem(time (dirac_ref ~= 0), dirac_ref (dirac_ref ~= 0) - y_shift, 'filled', 'b');
set(stem2, 'BaseValue', -y_shift, 'ShowBaseLine', 'off');
legend(sprintf('t = %.6f', t(k)));
grid on
xlim([c d]);
xlabel('x');
ylim([-2 5]);
pause(0.00001);
hold off
frame = getframe(gcf);
writeVideo(v, frame);
end
close(v);

Answers (2)

Dan
Dan on 5 Nov 2020
It seems that all the stem plots on the same axes are beholden to a common basevalue. This is step back from earlier versions of MATLAB ... to show two basevalues on the same axes, you can lay two axes on top of one another:
figure;
x = axes;
ylim([0,21])
xlim([0,11])
hold on
a=stem(1:10,'basevalue',10);
hold on
y = axes('position',x.Position);
ylim([0,21])
xlim([0,11])
set(y,'Color', 'none')
hold on
b=stem(11:20,'basevalue',20);
To get this plot:

VBBV
VBBV on 18 Sep 2020
Edited: VBBV on 18 Sep 2020
%if true
% code
% end
for k = 1:10:m % for faster animations i.e. skip frame
z0 = u_approx_dirac(k,:);
z1 = dirac(k,:);
clf
hold on
plot(x, zeta(x), 'r'); _italic_
plot(x_dirac , z0 , 'k');
stem1 = stem(x (z1 ~= 0), z1 (z1 ~= 0), 'filled', 'k');
set(stem1, 'LineWidth', 2, 'BaseValue', 0, 'ShowBaseLine', 'off');
hold on %....
time = t(1:2:k);
ref = reflectedwave(y(1:2:k));
dirac_ref = dirac_reflectedwave(y(2:2:k));
plot(time, ref - y_shift, 'b');
stem2 = stem(time (dirac_ref ~= 0), dirac_ref (dirac_ref ~= 0) - y_shift, 'filled', 'b');
set(stem2, 'BaseValue', -y_shift, 'ShowBaseLine', 'off');
legend(sprintf('t = %.6f', t(k)));
grid on
xlim([c d]);
xlabel('x');
ylim([-2 5]);
pause(0.00001);
%hold off
frame = getframe(gcf);
writeVideo(v, frame);
end
close(v);
You can either delete hold off at end of for loop or add hold on between stem1 and stem2 plots
  2 Comments
VBBV
VBBV on 14 Nov 2020
%if true
% code
%end
for k = 1:10:m % for faster animations i.e. skip frame
z0 = u_approx_dirac(k,:);
z1 = dirac(k,:);
figure(1)
%subplot(211)
plot(x, zeta(x), 'r');
hold on
plot(x_dirac , z0 , 'k');
hold on
stem1 = stem(x (z1 ~= 0), z1 (z1 ~= 0), 'filled', 'k');
set(stem1, 'LineWidth', 2, 'BaseValue', 0, 'ShowBaseLine', 'off');
time = t(1:2:k);
ref = reflectedwave(y(1:2:k));
dirac_ref = dirac_reflectedwave(y(2:2:k));
figure(2)
%subplot(212)
plot(time, ref - y_shift, 'b');
hold on
stem2 = stem(time (dirac_ref ~= 0), dirac_ref (dirac_ref ~= 0) - y_shift, 'filled', 'b');
set(stem2, 'BaseValue', -y_shift, 'ShowBaseLine', 'off');
legend(sprintf('t = %.6f', t(k)));
grid on
xlim([c d]);
xlabel('x');
ylim([-2 5]);
pause(0.00001);
%hold off
frame = getframe(gcf);
writeVideo(v, frame);
end
close(v);
Use figure(1) and figure(2) for two different base values as above. Or subplot(211) for figure(1) and subplot(212) for figure(2) if you don't want values be displayed in two separate figures. You have multiple plot commands between two stem plots.

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by