Multiple colors in the same line
问题 I would like to plot a sine curve in Matlab. But I want it blue for the positive values and red for the negative values. The following code just makes everything red... x = []; y = []; for i = -180 : 180 x = [x i]; y = [y sin(i*pi/180)]; end p = plot(x, y) set(p, 'Color', 'red') 回答1: Plot 2 lines with different colours, and NaN values at the positive/negative regions % Let's vectorise your code for efficiency too! x = -pi:0.01:pi; % Linearly spaced x between -pi and pi y = sin(x); % Compute