Question

I have an array of data that I would like to plot

temp=0.5*rand(500,10);
[~,offset]=meshgrid(1:500,1:10);
figure(101)
plot(temp+offset')

How can I set the line style to automatically change to the next style once the line colours have been through one cycle?

For this example I would like the 8-10th lines to have a different line style. I can do it manually but I'd like Matlab to do it for me if I can set a default option somewhere.

Was it helpful?

Solution

Your first inclination might be to just change the 'LineStyleOrder' property of the axes before plotting your data. Unfortunately, high-level plotting functions like PLOT will reset the 'LineStyleOrder' property of the axes to it's default value '-'before plotting. One solution is to change the default value used by all axes objects at the root level. For example:

set(0,'DefaultAxesLineStyleOrder',{'-',':'});

Will first use a solid line, then a dotted line, and then repeat again if necessary for each plot. Note that you could also use a custom 'ColorOrder' property with high-level plotting functions by changing the default value at the root as well. The following example will change it so PLOT cycles between only red, green, and blue:

set(0,'DefaultAxesColorOrder',[1 0 0; 0 1 0; 0 0 1]);

Instead of worrying about different line styles, another solution to your problem would be to set the default color order to have more than just 7 colors.

Once default property values on the root are set, they will stay that way until MATLAB is closed. When reopened, the default property values will be set back to their factory-defined values. Commands like CLEAR won't set default properties back to their factory-defined values. Instead, you should set the default property value to 'remove' to undo user-defined values, like so:

set(0,'DefaultAxesLineStyleOrder','remove');  %# Sets the default back to '-'

As another alternative to changing the default properties used by all axes objects, if you change the NextPlot property of an individual axes to anything except 'replace' you can then change the 'LineStyleOrder' or 'ColorOrder' properties to whatever you want and PLOT will not reset them to their defaults. For example, this should do what you want as well:

set(gca,'NextPlot','add','LineStyleOrder',{'-',':'});
plot(temp+offset');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top