How to manipulate Plot Legend to show markers and colours seperatly?
173 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (1)
Dr. Seis
el 25 de Sept. de 2012
Editada: Dr. Seis
el 25 de Sept. de 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')
4 comentarios
ANetTow
el 26 de Sept. de 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
Mohammad Saiful Islam
el 27 de Sept. de 2016
Thanks for your code. But i am facing some problem during plot. Mathworks shows me the line 22 have some error. They shows the following message: Error using line There is no Line property on the Line class.
Error in diff_color_legend_2 (line 22) l1(i, j) = line(w, z, 'Color', colors(i, :), 'Marker', marks{j}, 'Line', 'none'); How can I solve this problem?
Ver también
Categorías
Más información sobre Legend en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!