MATLAB Answers

Add legend to plot colored by colormap function

79 views (last 30 days)
Sophie Leiter
Sophie Leiter on 12 Aug 2021
Commented: Sophie Leiter on 12 Aug 2021
I am using currently making a plot where the color of the points is determined by a code (1-3) in the 3 column of the matrix. I can't seem to figure out how to get matlab to make a legend for this and not a colorbar. Since they're plotted as one thing the automatic legend function only includes one point. As it stands I've just been adding a legend in illustrator but it's a bit time consuming with lots of graphs and I'd prefer to have it done in matlab. Is it possible to either make a legend from scratch and specify each entry and label or get matlab to do it automatically? Thanks!
The code I am using and the figure are below:
x = [HL_conpor HL_perm HL_class];
colors = [0.8 0.8 0;
1 0.5 0
1 0 0
]; %
scatter(x(:,1), x(:,2),[], x(:,3),'filled')
set(gca, 'YScale', 'log')
xlabel('Connected Porosity(\phi_c)');
ylabel('Permeability (m^2)');
legend_labels = {'VDP1', 'VDP2', 'VDP3'};
legend(legend_labels, 'Location', 'Best');

Accepted Answer

Dave B
Dave B on 12 Aug 2021
Edited: Dave B on 12 Aug 2021
When you specify CData (the color input) for scatter, MATLAB uses colormapping to plot the data, and a colorbar to describe the mapping. Scatter is thinking of CData as continuous, but you're thinking of color as discrete.
There are two common approaches to solving this problem...
Problem case and fake data
colormap([1 0 0; 0 1 0; 0 0 1])
colorbar('Ticks',[4/3 2 8/3],'TickLabels',["Red" "Green" "Blue"])
title('Problem Version')
Solution 1: use hold on, make seperate scatter objects, and use colororder to define the colors (could also specify the color explicitly in each scatter as long as there's one color for each scatter):
hold on
colororder([1 0 0;0 1 0; 0 0 1])
legend(["Red" "Green" "Blue"])
title("3 Scatters","(colororder instead of colormap)")
Solution 2: create some hidden scatters and pass them in to legend:
hold on
legend(h, ["Red" "Green" "Blue"])
title("3 Hidden Scatters")
For more info on colororder, see this documentation page
  1 Comment
Sophie Leiter
Sophie Leiter on 12 Aug 2021
That makes sense. Thank you for the explanation!

Sign in to comment.

More Answers (1)

Scott MacKenzie
Scott MacKenzie on 12 Aug 2021
One approach is to do three scatters, one for each value in the 3rd column in your data. Here's the general idea using a modified version of your code:
x = [rand(25,1) rand(25,1) randi(3,25,1)];
colors = [0.8 0.8 0; 1 0.5 0; 1 0 1];
c1 = x(:,3) == 1;
c2 = x(:,3) == 2;
c3 = x(:,3) == 3;
scatter(x(c1,1), x(c1,2),100, 'filled');
hold on;
scatter(x(c2,1), x(c2,2),100, 'filled');
scatter(x(c3,1), x(c3,2),100, 'filled');
set(gca, 'YScale', 'log')
xlabel('Connected Porosity(\phi_c)');
ylabel('Permeability (m^2)');
legend_labels = {'VDP1', 'VDP2', 'VDP3'};
legend(legend_labels, 'Location', 'Best');




Community Treasure Hunt

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

Start Hunting!

Translated by