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

355 views (last 30 days)
Jakob on 25 Sep 2012
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,

Answers (1)

Dr. Seis
Dr. Seis on 25 Sep 2012
Edited: Dr. Seis 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]) + ...
calc_data_2 = cos(2*pi*[sample_points;sample_points;sample_points]) + ...
% 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_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')
Mohammad Saiful Islam
Mohammad Saiful Islam on 27 Sep 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?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!