Search code examples
matlabplotcolorsmatlab-figure

How to change line colors in Matlab plot?


A beginner's question... yet I didn't find anything in the help/ online! How do I create a plot in Matlab, in which one line is black and one line is gray and dashed? The best code I could come up with is posted below, but it doesn't work...

figure;
plot( datevector, data1, 'color', [0 0 0],...
      datevector, data2, '--', 'color', [0.5 0.5 0.5],...
      'LineWidth',1.2 );

Solution

  • From the Matlab documentation:

    plot(___,Name,Value) specifies line properties using one or more Name,Value pair arguments. Use this option with any of the input argument combinations in the previous syntaxes. Name,Value pair settings apply to all the lines plotted. You cannot specify different Name,Value pairs for each line using this syntax.

    You can use hold on to plot multiple lines with different properties:

    plot( datevector, data1, 'Color', [0 0 0]);
    hold on
    plot(datevector, data2, 'LineStyle','--', 'Color', [0.5 0.5 0.5],'LineWidth',1.2 );