Multiple legends with colors

21 views (last 30 days)
john
john on 26 Mar 2013
Hi, if k is 7, then colors for legends are repeating for each leg. How can I generate different color for each value in plot? ( different color or type of legend). Or it is possible to make for leg1 different colors with -b, for leg2 the same colors like for leg1 but with -ro.
This is my code:
for k=1:7;
leg1{k}=sprintf('speed_%d',k);
leg2{k}=sprintf('acceler_%d',k);
end
for k=1:7
leg3{k}=sprintf('distance_%d',k);
end
figure
plot(t',speed,t',acceler_vet,t',distance);
legend = legend([leg1,leg2,leg3]);
Please for help

Accepted Answer

Image Analyst
Image Analyst on 26 Mar 2013
See my ColorOrder demo. You can get more colors.
% Unless you specify the 'Color' property when you plot,
% plots are plotted according to the 'ColorOrder' property of the axes.
% This demo shows how you can change the default color order of plots.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 18;
% Make 20 plots, with 25 data points in each plot.
numberOfDataSets = 20;
x = 1:25;
y = rand(numberOfDataSets, length(x));
% These y would all be on top of each other.
% Separate the plots vertically.
offsets = repmat((1:numberOfDataSets)', [1, length(x)]);
y = y + offsets;
% Get the initial set of default plot colors.
initialColorOrder = get(gca,'ColorOrder') % Initial
% See what the colors look like when plotted:
subplot(2, 1, 1);
plot(x,y, 'LineWidth', 3);
grid on;
caption = sprintf('%d plots with the Initial Default Color Order (Note the repeating colors)', numberOfDataSets);
title(caption, 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
% Give a name to the title bar.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
choice = menu('Which ColorOrder do you want?', 'jet', 'random', 'hsv', 'hot', 'cool', 'spring', 'summer',...
'autumn', 'winter', 'lines', 'gray', 'bone', 'copper', 'pink');
% Make a new axes:
subplot(2, 1, 2);
% Create a new colormap that will define the new default color order property.
switch choice
case 1
newDefaultColors = jet(numberOfDataSets);
case 2
newDefaultColors = rand(numberOfDataSets, 3);
case 3
newDefaultColors = hsv(numberOfDataSets);
case 4
newDefaultColors = hot(numberOfDataSets);
case 5
newDefaultColors = cool(numberOfDataSets);
case 6
newDefaultColors = spring(numberOfDataSets);
case 7
newDefaultColors = summer(numberOfDataSets);
case 8
newDefaultColors = autumn(numberOfDataSets);
case 9
newDefaultColors = winter(numberOfDataSets);
case 10
newDefaultColors = lines(numberOfDataSets);
case 11
newDefaultColors = gray(numberOfDataSets);
case 12
newDefaultColors = bone(numberOfDataSets);
case 13
newDefaultColors = copper(numberOfDataSets);
otherwise
newDefaultColors = pink(numberOfDataSets);
end
% Note: You can build your own custom order if you want,
% just make up a array with numberOfDataSets rows and 3 columns
% where each element is in the range 0-1.
% Apply the new default colors to the current axes.
set(gca, 'ColorOrder', newDefaultColors, 'NextPlot', 'replacechildren');
% Now get the new set of default plot colors.
% Verify it changed by printing out the new default color set to the command window.
newColorOrder = get(gca,'ColorOrder')
% Now plot the datasets with the changed default colors.
plot(x,y, 'LineWidth', 3);
grid on;
caption = sprintf('%d plots with the New Default Color Order', numberOfDataSets);
title(caption, 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
msgbox('Done with ColorOrder demo!');
  8 Comments
Image Analyst
Image Analyst on 10 Apr 2013
I suggest that the easiest way is to just call "cla reset" to erase everything, and just plot the desired plots.
john
john on 11 Apr 2013
If I use this code:
for i=1:7
handles.speed(:,i)=(s/t)';
plot(t',handles.speed);
cla(handles.speed(:,1),'reset');
end;
I gor error Bad handle

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!