MATLAB Answers

Two Stem Plots with Different Base Values on the Same Graph

5 views (last 30 days)
Chien-Cheng Chiu
Chien-Cheng Chiu on 18 Sep 2020
Commented: Chien-Cheng Chiu on 18 Sep 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);

  0 Comments

Sign in to comment.

Answers (1)

Vasishta Bhargava
Vasishta Bhargava on 18 Sep 2020
Edited: Vasishta Bhargava 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

  1 Comment

Chien-Cheng Chiu
Chien-Cheng Chiu on 18 Sep 2020
I tried commenting out "hold off" and also tried between "hold on" between stem1 and stem2 plots, and also tried both at the same time. I am still getting the same problem

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