Create Own Legend With Colors and Boxes

9 views (last 30 days)
So basically what I've done is created a figure that shows the relative densities of a bunch of point cloud data. Then using getrect I will be able to have the user click and drag a smaller sample area. The only thing I want to do to enhance this is to create a legend which would basically be of the form:
White 0
Red 1 - 6400
Orange 6400 - 80000
Yellow 80001 - 500000
Green 500001 or More
Now when I plotted these areas I did not use a typical method. I ran a loop that determined where to plot an area, and colored the area according to the specifications listed above. I don't have a data set for which to throw into a legend function.

Accepted Answer

Kelly Kearney
Kelly Kearney on 25 Jan 2016
There are two methods that come to mind...
  1. Use a regular colorbar, and change the tick labels to match your color intervals.
  2. Create hidden patch objects, and use those in the legend.
Both methods used below:
[x,y,z] = peaks;
z = max(peaks* 100000, 0);
cmap = [1 1 1; 1 0 0; 1 .5 0; 1 1 0; 0 1 0];
lbl = {'0', '1-6400','6400-80000', '80001-500000', '500000+'};
[n,bin] = histc(z, [0 1 6400 80000 500000 Inf]);
pcolor(x,y,bin);
colormap(cmap);
A legend with hidden patches:
for ii = 1:size(cmap,1)
p(ii) = patch(NaN, NaN, cmap(ii,:));
end
legend(p, lbl);
Or a colorbar with relabeled ticks:
cb = colorbar;
set(cb, 'ticks', 1:5, 'ticklabels', lbl);
set(gca, 'clim', [0.5 5.5]);
  2 Comments
Michael Barrow
Michael Barrow on 25 Jul 2019
really useful for creating a custom contourf() legend

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!