## How to manipulate Plot Legend to show markers and colours seperatly?

### Jakob (view profile)

on 25 Sep 2012
Latest activity Commented on by Mohammad Saiful Islam

### Mohammad Saiful Islam (view profile)

on 27 Sep 2016
Hi there everyone!
I want to display the results of a numerical computation in which I varyed two discretisation parameters.
Now I want to show in which way any parameter effects the results. By using the same color I want to show the lines representing calculations with the same number of discrete elements. And by using the same marker I want to show the lines that represent calculations with the same number of time steps.
I got as far as changing the colors and markers in the plot itself goes, but now I need a legend that clearly displays the correlations of color and marker. My legend contains one entry for every line which is not useful. I want a legend that first shows:
- this color means that many elements
- this marker means that many time steps.
Is it possible to tweak the legend like that?
Your help will be greatly appreciated! Thank you,
-Jakob.

### Dr. Seis (view profile)

on 25 Sep 2012
Edited by Dr. Seis

### Dr. Seis (view profile)

on 25 Sep 2012

It can be done... you will have to do some finagling, but it can be done. Here is an example where we have 2 different datasets representing 2 different "elements" and each dataset has 3 different sets of data representing 3 different "time steps":
% Generate random data
sample_points = 0:0.1:10;
calc_data_1 = sin(2*pi*[sample_points;sample_points;sample_points]) + ...
0.1*randn(3,101);
calc_data_2 = cos(2*pi*[sample_points;sample_points;sample_points]) + ...
0.1*randn(3,101);
% Generate dummy info for plot handles "h"
h = zeros(5,1);
h(1) = plot(sample_points,calc_data_1(1,:),'r-'); hold on;
h(2) = plot(sample_points,calc_data_2(1,:),'b-');
h(3) = plot(sample_points,calc_data_1(1,:),'ks');
h(4) = plot(sample_points,calc_data_1(2,:),'kd');
h(5) = plot(sample_points,calc_data_1(3,:),'ko');
% Plot things the right way
plot(sample_points,calc_data_1(1,:),'rs-');
plot(sample_points,calc_data_1(2,:),'rd-');
plot(sample_points,calc_data_1(3,:),'ro-');
plot(sample_points,calc_data_2(1,:),'bs-');
plot(sample_points,calc_data_2(2,:),'bd-');
plot(sample_points,calc_data_2(3,:),'bo-'); hold off;
% Define legend according to handles "h"
legend(h,'M elements used','N elements used','P time steps','Q time steps','R time steps')

Show 1 older comment
Jakob

### Jakob (view profile)

on 25 Sep 2012
Thanks alot Elige! I'll try that. Sounds simple and proper. I was thinking about creating a 'fake' plot and using its legend but I am not comfortable with handles so your little example there is great help.
ANetTow

### ANetTow (view profile)

on 26 Sep 2012
I have a similar but different problem. I have model data over a wide range of parameters and I use colors and markers to distinguish values of the two parameters.
I use "line" to plot each point, make a legend for the colors across the top, and then attempt to make separate legend on the right for the markers by placing another axis on top of the first one. Even with the added axis, my second legend eliminates the first. This can be confirmed by comparing the figure children to the axes and legend handles.
I tried adding a second "line" command identical to the first but with a different handle to call for the second legend, but it doesn't help.
Any ideas? I am using R2012b on Windows XP. A minimal working example is provided below.
close all; clear all;
x = 1:3;
n = length(x);
y = 1:4;
m = length(y);
l1 = zeros(n, m);
l2 = zeros(n, m);
leg_label1 = cell(n, 1);
leg_label2 = cell(m, 1);
colors = [1 0 0;
0 1 0;
0 0 1];
marks = {'s';
'o';
'p';
'*'};
figure(1)
for i = 1:n
for j = 1:m
z = x(i)*cos(pi*y(j)/2);
w = x(i)*sin(pi*y(j)/2);
l1(i, j) = line(w, z, 'Color', colors(i, :), 'Marker', marks{j}, 'Line', 'none');
l2(i, j) = line(w, z, 'Color', colors(i, :), 'Marker', marks{j}, 'Line', 'none');
leg_label2{j} = ['y = ', num2str(y(j))];
end
leg_label1{i} = ['x = ', num2str(x(i))];
end
ax1 = get(1, 'CurrentAxes');
xlabel('w'); ylabel('z')
leg1 = legend(l1(:, 1), leg_label1);
set(leg1, 'Location', 'NorthOutside', 'Orientation', 'Horizontal', 'box', 'off')
ch1 = get(leg1, 'Children');
set(ch1(1:3:end), 'Marker', '.', 'MarkerSize', 10) % Color only
ax2 = axes('Position', get(ax1, 'Position'), 'Color', 'none', 'Visible', 'off');
leg2 = legend(l2(1, :), leg_label2);
set(leg2, 'Location', 'EastOutside', 'box', 'off')
ch2 = get(leg2, 'Children');
set(ch2(1:3:end), 'Color', 'k') % Marker only
disp(get(1, 'Children'))
disp([ax1; leg1; ax2; leg2])
end