- Use a regular colorbar, and change the tick labels to match your color intervals.
- Create hidden patch objects, and use those in the legend.
Create Own Legend With Colors and Boxes
9 views (last 30 days)
Show older comments
Dustin Gutsche
on 25 Jan 2016
Commented: Michael Barrow
on 25 Jul 2019
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.
0 Comments
Accepted Answer
Kelly Kearney
on 25 Jan 2016
There are two methods that come to mind...
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
More Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!