recipes : Plotting : Plot Manipulation : Setting line properties the tidy way

Problem

You have many plot commands with similar line properties and are tired of cluttering your scripts with all this redundant text.

Solution

Whenever you find yourself typing the same thing again and again, chances are are there is a more simple way. In the case of plots, here it is:

sty={'o--','Color', [1,1,1]*0.5, 'MarkerFaceColor',...
     [1,0.5,0.5],'MarkerSize',12, 'LineWidth',2}

clf
hold on
plot(randn(1,10),sty{:})
plot(randn(1,10),sty{:})
plot(randn(1,10),sty{:})
hold off

%For reference, the alternative would have been:
clf
hold on
plot(randn(1,10),'o--','Color',[1,1,1]*0.5,'MarkerFaceColor',[1,0.5,0.5],...
     'MarkerSize',12,'LineWidth',2)
plot(randn(1,10),'o--','Color',[1,1,1]*0.5,'MarkerFaceColor',[1,0.5,0.5],...
     'MarkerSize',12,'LineWidth',2)
plot(randn(1,10),'o--','Color',[1,1,1]*0.5,'MarkerFaceColor',[1,0.5,0.5],...
     'MarkerSize',12,'LineWidth',2)
hold off

The alternative, long, way takes more typing, looks messier, and harder to maintain. Here's another way to do the same thing:


clf
hold on
p(1)=plot(randn(1,10),'o--');
p(2)=plot(randn(1,10),'o--');
p(3)=plot(randn(1,10),'o--');
hold off

set(p,'Color', [1,1,1]*0.5, 'MarkerFaceColor', [1,0.5,0.5],...
     'MarkerSize',12, 'LineWidth', 2)

Discussion

Both approaches do the same thing. Which is easier will depend on your style and the exact application. Neither is more "correct" than the other. Setting the line properties as shown in the second example looks neater and is easier to work with, since changing the text in one location will modify all the lines.

Finally, there is a yet neater way that doesn't require the hold command or calling plot three different times. Here it is:


clf
x=1:10;
p=plot(x,randn(1,10), x,randn(1,10), x,randn(1,10), 'o--');
set(p,'Color', [1,1,1]*0.5, 'MarkerFaceColor', [1,0.5,0.5],...
     'MarkerSize',12, 'LineWidth', 2)

This final method is often overlooked, possibly because it's buried in the middle of the help text for the plot command.