MATLAB: Line colour of two y-axis plot

plotyyaxis

Hi All,
I am plotting multiple lines for y-axis 1 and another line as y-axis 2 using the yyaxis command. If I plot only the first set of data as a single plot with one y-axis, the lines are plotted with different colours. But when I plot using yyaxis left for one set of data, and plot another line for yyaxis left, the data plotted against the first y-axis comes out all the same colour which is not what I want. From below, if I only run 'plot(xdata,y1data)' I get 10 lines all different colours. But if I run from 'yyaxis left' to the end I get 10 lines of the same colour (with symbols which I don't want), and one line of a different colour plotted against the second y-axis. How do I get the multi-colour lines back for the first plot? Thanks.
y1data = magic(10);
y2data = randi([-10 10],10,1);
xdata = [1:10]';
yyaxis left
plot(xdata,y1data);
yyaxis right
plot(xdata, y2data);

Best Answer

Set the LineStyleOrder and ColorOrder properties after selecting the Left/Right yyaxes
y1data = magic(10);
y2data = randi([-10 10],10,1);
xdata = [1:10]';
yyaxis left
set(gca, 'LineStyleOrder', '-', 'ColorOrder', jet(10))
plot(xdata,y1data);
yyaxis right
set(gca, 'LineStyleOrder', '-', 'ColorOrder', jet(10))
plot(xdata, y2data);
You can replace the jet colormap with any other colormap or you can create your own colormap by using an nx3 matrix of RGB values.
You can replace the '-' with any line style or a cell array of line styles to cycle through.
Related Question