Indicate name of filled areas inside area plot - Matlab?

156 Views Asked by At

It's difficult to identify in this area plot each one of the many filled areas by just looking at the legend (it's like 16!).

So, I was wondering if there's a way to place some sort of labels (with a pointer perhaps?) inside the plot that clearly identifies each filled areas?

Thanks!

enter image description here

2

There are 2 best solutions below

1
On BEST ANSWER

Here is an alternative using annotation objects and the textarrow option, which displays text along with an arrow. This could be useful to you to point in narrow regions where text would hide data.

For example:

clear
clc
close all

x = 1:128;

%// Plot data
figure
hAxes1 = axes('Position',[.1 .1 .6 .8]);
image(x.');
axis off
colormap(jet(128))

%// Define labels
ColorLabels = {'Red';'Orange';'Green';'Blue';'More blue'};

%// Define starting and ending x and y positions
xstart = .8;
xend = .6;
ystart = linspace(.1,.8,numel(ColorLabels));
yend = linspace(.15,.8,numel(ColorLabels));
for k = 1:numel(ColorLabels)

    annotation('textarrow', [xstart xend],[ystart(k) yend(k)],...
           'String', ColorLabels{k});    
end

gives the following output:

enter image description here

0
On

There may be a builtin way that I don't know about but I find the lower-level text function is usually the best answer for this kind of thing.

It'll require some housekeeping - you have to supply an x and y coordinate and the text to print, e.g.

text(20, 400, 'Region 4')

which will centre the label on (20,400) (see the 'VerticalAlignment' and 'HorizontalAlignment' name-value pairs in the docs for fine-tuning), so for me it's usually preferable to write a small wrapper that will work them out from the data. Of course this is usually specific to the particular type of data you're using and rarely generalises to other uses of area plots, but that's most likely why there isn't already a generic labelling function (that I'm aware of).