Last color on legend does not match last color on stem plot, after manually setting color in for loops.
This code generates stem plots for functions of number sequences. The last two plots require for loops to shift the sequences. The plot colors are hard coded in each loop to keep the same color. This works for the plots, but the legend shows the last two funcitons as both green when the last one should be purple. What might be causing this and is there a work-around?
x = [0,1,2,3,4,-4,-3,-2,-1,0];
y = [0,0,-1,0,1,0,-1,0,1,0,0];
longest_sequence = max(numel(x),numel(y));
figure(1)
hold on
stem(x,’LineWidth’,2.0)
stem(5*x,’LineWidth’,2.0)
stem(y,’LineWidth’,2.0)
for n = 2:9
stem(n-1,y(n+1),’LineWidth’,2.0, ‘Color’,’#77AC30′)
end
for n = 2:9
stem(n-1,-2*x(n-1)+3*y(n+2),’LineWidth’,2.0,’Color’,’#7E2F8E’)
end
hold off
title(‘Problem 1’)
xlim([0 longest_sequence+1])
xticks(0:1:11)
yticks(-20:1:20)
xlabel(‘Time’)
ylabel(‘Amplitude’)
set(gca,’FontWeight’,’bold’)
legend(‘x’,’5x’,’y’,’y(n+1)’,’-2x(n-1)+3y(n+2)’)This code generates stem plots for functions of number sequences. The last two plots require for loops to shift the sequences. The plot colors are hard coded in each loop to keep the same color. This works for the plots, but the legend shows the last two funcitons as both green when the last one should be purple. What might be causing this and is there a work-around?
x = [0,1,2,3,4,-4,-3,-2,-1,0];
y = [0,0,-1,0,1,0,-1,0,1,0,0];
longest_sequence = max(numel(x),numel(y));
figure(1)
hold on
stem(x,’LineWidth’,2.0)
stem(5*x,’LineWidth’,2.0)
stem(y,’LineWidth’,2.0)
for n = 2:9
stem(n-1,y(n+1),’LineWidth’,2.0, ‘Color’,’#77AC30′)
end
for n = 2:9
stem(n-1,-2*x(n-1)+3*y(n+2),’LineWidth’,2.0,’Color’,’#7E2F8E’)
end
hold off
title(‘Problem 1’)
xlim([0 longest_sequence+1])
xticks(0:1:11)
yticks(-20:1:20)
xlabel(‘Time’)
ylabel(‘Amplitude’)
set(gca,’FontWeight’,’bold’)
legend(‘x’,’5x’,’y’,’y(n+1)’,’-2x(n-1)+3y(n+2)’) This code generates stem plots for functions of number sequences. The last two plots require for loops to shift the sequences. The plot colors are hard coded in each loop to keep the same color. This works for the plots, but the legend shows the last two funcitons as both green when the last one should be purple. What might be causing this and is there a work-around?
x = [0,1,2,3,4,-4,-3,-2,-1,0];
y = [0,0,-1,0,1,0,-1,0,1,0,0];
longest_sequence = max(numel(x),numel(y));
figure(1)
hold on
stem(x,’LineWidth’,2.0)
stem(5*x,’LineWidth’,2.0)
stem(y,’LineWidth’,2.0)
for n = 2:9
stem(n-1,y(n+1),’LineWidth’,2.0, ‘Color’,’#77AC30′)
end
for n = 2:9
stem(n-1,-2*x(n-1)+3*y(n+2),’LineWidth’,2.0,’Color’,’#7E2F8E’)
end
hold off
title(‘Problem 1’)
xlim([0 longest_sequence+1])
xticks(0:1:11)
yticks(-20:1:20)
xlabel(‘Time’)
ylabel(‘Amplitude’)
set(gca,’FontWeight’,’bold’)
legend(‘x’,’5x’,’y’,’y(n+1)’,’-2x(n-1)+3y(n+2)’) color, legend, for loop MATLAB Answers — New Questions