COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### iterative plotting: changing color/linestyle within a loop

• Email
• Follow

```Hi,
I'm trying to perform iterative plotting while changing the color/linestyle within each iteration.  Since the number of plots may vary I have created a string matrix which allows for five differnt plot styles.

My current code is:
plot_type = ['''r*-''';'''g+-''';'''b*-''';'''c+-''';'''m*-'''];
%where plot_type(1,1:5) = 'r*-'

for i = 1:k
plot( XX , plt_matrix(i,:) , plot_type(i,1:3) )
end

Any ideas would be appreciated.
Thanks
```
 0

See related articles to this posting

```On 10-12-16 05:22 PM, John wrote:
> Hi,
> I'm trying to perform iterative plotting while changing the color/linestyle
> within each iteration. Since the number of plots may vary I have created a
> string matrix which allows for five differnt plot styles.
>
> My current code is:
> plot_type = ['''r*-''';'''g+-''';'''b*-''';'''c+-''';'''m*-'''];
> %where plot_type(1,1:5) = 'r*-'
>
> for i = 1:k
> plot( XX , plt_matrix(i,:) , plot_type(i,1:3) )
> end
>
> Any ideas would be appreciated. Thanks

Too many apostrophes.

plot_type = ['r*-'; 'g+-'; 'b*-'; 'c+-'; 'm*-'];

plot(XX, plt_matrix(i,:), plot_type(i,:))

```
 0

```What you're doing looks OK - I'm not sure what kind of ideas you need.

Still, in case they are any use, here are a couple of functions I've been using for this sort of thing. Both are meant to be used with "hold" switched on. The context for me is display of matching features in stereo image pairs, so they'll certainly need modifying for line plots.

function different_points(x, y)
% Display a lot of points in different colour/symbol combinations

% Vectors for colour/symbol combinations to distinguish matches
colours = ['y';'m';'c';'r';'g';'b'];
syms = ['o';'x';'s';'+';'d';'*';'v';'p'];
lc = length(colours);
ls = length(syms);

for i=1:length(x)
colsym = [colours(mod(i-1,lc)+1), syms(mod(i-1,ls)+1)];
plot(x(i), y(i), colsym);
end
end

function disp_vecs(corrs)
% Display disparity vectors using a variety of colours

colours = ['y';'m';'c';'r';'g';'b'];
lc = length(colours);
[nr, nmatches] = size(corrs);

for i=1:nmatches
colsym = [colours(mod(i-1,lc)+1), '-'];
plot([corrs(2,i); corrs(4,i)], [corrs(1,i); corrs(3,i)], colsym);
end
end
```
 0

```Walter Roberson <roberson@hushmail.com> wrote in message <iee7qu\$ksn\$1@nrc-news.nrc.ca>...
> ...
>
> Too many apostrophes.
> ...

Oops! I missed the obvious problem! I thought John was literally inviting ideas to extend his code, and didn't realise that he was saying that it doesn't work.

John: put the error message in please!
```
 0

```On 10-12-16 05:54 PM, David Young wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <iee7qu\$ksn\$1@nrc-news.nrc.ca>...
>> ...
>>
>> Too many apostrophes.
>> ...
>
> Oops! I missed the obvious problem! I thought John was literally inviting
> ideas to extend his code, and didn't realise that he was saying that it
> doesn't work.
>
> John: put the error message in please!

Ah, if he was looking for ideas to extend his code, then I would suggest he
use the FEX contribution 'plt', which has already been designed to allow
combinations of line attributes.
```
 0

```Sorry for the confusion.  Yes, I was getting an error message.  I used David's approach and everything works!  Too many '''apostrophes''' !!!

colors   = ['r';'g';'b';'c';'m'];
symbols = ['*-';'+-';'*-';'+-';'*-'];

hold on
for i = 1:k
colsym = [colors(i),symbols(i,1:2)];
plot(XX,plt_matrix(i,:), colsym)
end

Thanks
```
 0

```
"John " <jahewe01@louisville.edu> wrote in message
news:iee6us\$lp1\$1@fred.mathworks.com...
> Hi,
> I'm trying to perform iterative plotting while changing the
> color/linestyle within each iteration.  Since the number of plots may vary
> I have created a string matrix which allows for five differnt plot styles.
>
> My current code is:
> plot_type = ['''r*-''';'''g+-''';'''b*-''';'''c+-''';'''m*-'''];
> %where plot_type(1,1:5) = 'r*-'
>
> for i = 1:k
>     plot( XX , plt_matrix(i,:) , plot_type(i,1:3) )
> end
>
> Any ideas would be appreciated.  Thanks

Rather than using char arrays, I would use a cell array of strings.

plot_types = {'r*-', 'g+-', 'b*-', 'c+-', 'm*-'};
x = 0:0.1:2*pi;
h = zeros(1, numel(plot_types));
axis([0 2*pi -1 1]);
hold on
for k = 1:numel(plot_types)
h(k) = plot(x, sin(k*x), plot_types{k}, 'DisplayName',
sprintf('sin(%d*x)', k));
end
legend(h)

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
http://www.mathworks.com

```
 0