Ken & Mike on the MATLAB Desktop

August 25th, 2008

Overlaying Information on a Plot

On a previous posting entitled Focused on Zooming, Bao posed a great question that I thought would be useful to answer in a separate post. The question concerned the ability to overlay data on a plot. At its simplest, an overlay can be implemented in terms of placing a non-visible axes above the visible one. There are, however different scenarios for needing an overlay. In this post, I’ll examine the different overlays and how you might go about creating them in MATLAB.

Contents

Decorating an Object

One of the most common reasons for an overlay is to draw attention to a plot of interest. For example, if we have four subplots containing data we may wish to draw attention to one of the subplots over the others.

figure;
subplot(2,2,1);
plot(rand(3));
interestingAxes = subplot(2,2,2);
plot(magic(3));
subplot(2,2,3);
plot(rand(3));
subplot(2,2,4);
plot(rand(3));

In the above plot, there is nothing to indicate whether one axes is more important than another. It would be useful to highlight the axes we wish to emphasize.

We begin by creating a non-visible axes whose position covers the whole figure.

invisibleAxes = axes(‘Visible’,‘off’,‘Position’,[0 0 1 1],
    ‘XLim’,[0 1],‘YLim’,[0 1],‘HitTest’,‘off’);

Setting the XLim and YLim properties ensure that when we deal with coordinates normalized to the figure, they will also be normalized to the axes. The HitTest property tells tools such as ZOOM to ignore this axes when interacting with the figure.

We will now create a pink border around our interesting axes using the rectangle command:

border = rectangle(‘Parent’,invisibleAxes,
    ‘Position’,get(interestingAxes,‘Position’), 
    ‘EdgeColor’, [1 .4 .4],
    ‘LineWidth’,4,‘HitTest’,‘off’);

The pink rectangle now indicates which area of the figure may be important.

close;

Annotating a Plot

In some cases, the figure is too large a canvas on which to communicate. In this case, we may want to focus on aspects of the data. These annotations will appear on top of the data. For this case, we will use a smaller overlay axes. We will ensure that this axes has the same X and Y limits as the original plot using the linkaxes command.

figure;
dataAxes = axes;
plot(magic(3));
invisibleAxes = axes(‘Visible’,‘off’,
    ‘Position’,get(dataAxes,‘Position’),
    ‘HitTest’,‘off’,‘XLimMode’,‘manual’,‘YLimMode’,‘manual’);
linkaxes([dataAxes invisibleAxes]);

If we wish to draw attention to an area, we can now place information into the invisible axes using annotations.

rectangle(‘Parent’,invisibleAxes,‘Position’,[1.6 5 .2 1],
    ‘EdgeColor’,[1 .4 .4],‘LineWidth’,4);
text(‘Parent’,invisibleAxes,‘String’,‘No Data Here’,
    ‘HorizontalAlignment’,‘Center’,‘VerticalAlignment’,‘Bottom’,
    ‘Position’,[1.7 6 0]);

Since the limits have been linked, the pink box and the associated text will travel with the data when the plot is panned or zoomed.

zoom(2);

close;

Adding Transient Information

Sometimes, the information we wish to provide may be transient. For example, if a user clicks on a line, we may want to see more information about the line. After the user is satisfied, this information should go away. This sort of interaction model can also be implemented in terms of an overlay as well in conjunction with figure callbacks.

A more complex case may be that of a jeweler’s loupe tool. In this case, we want to create a visible axes that shows a zoomed in portion of the data being examined. The following code will create a loupe without the need for user interaction.

figure;
dataAxes = axes;
plot(magic(3));
% Compute the center of the axes:
dataAxesPosition = get(dataAxes,‘Position’);
xc = dataAxesPosition(1) + dataAxesPosition(3)/2;
yc = dataAxesPosition(2) + dataAxesPosition(4)/2;
% Use this information to compute the center of an overview:
loupeAxesPosition = [xc-.125, yc-.125, .25, .25];
loupeAxes = axes(‘Position’,loupeAxesPosition,
    ‘XTickLabel’,,‘YTickLabel’,,‘XTick’,[],‘YTick’,[],‘Box’,‘on’);
% Copy the children of the data axes over:
copyobj(get(dataAxes,‘Children’),loupeAxes);
% Zoom the loupe axes by a factor of 4:
zoom(loupeAxes,4);

A fully featured jeweler’s loupe tool which uses figure callbacks may be found on the File Exchange here.

In general, the axes can be used for more than displaying plots. It can also be used as a window into different aspects of the data. What sort of information have you tried to add to plots in the past?

2 Responses to “Overlaying Information on a Plot”

  1. Dr. Hanno Ix replied on :

    Hello -

    I am trying to make nomograms with Matlab.

    Is there a way to make all 4 sides of a plot for individual scales/rulers? If not please put this in your suggestion box.

    In general, I have a need to make all kinds of rulers, scales, graduations: straight, curved, various fonts, various directions of fonts, labels, a hierarchy of tick marks, foreign symbols, carious colors, etc, etc. Again, please give me a hint if there is a specialized matlab tool for this. If not then please accept this as another addition to your suggestion box.

    Bets regards

  2. Dan replied on :

    At the current time, we don’t have a way provided to add multiple rulers to a plot. We do supply two rulers with the PLOTYY command, and there is a PLOTXX function on the File Exchange. A plot with four rulers could conceivably be implemented in a way similar to these functions.

    Regarding the customization of rulers, we don’t currently have a solution for this, short of creating an invisible axes and manually adding the annotation you desire. I will make sure your suggestions are noted.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Ken & Mike work on the MATLAB Desktop team.
  • Ori: The current folder shortcut used to be alt-y. Now it is alt-o. However, while in the editor window, alt-o opens...
  • Jose Miguel: Hi First of all, thanks for the post, it is really useful. I am trying to develop a Java GUI within...
  • Ken: Hi Siddharth, There isn’t currently any way to move the docking controls. Feel free to submit an...
  • Phil: I have the same problem as described above with UITable working fine in the Matlab environment but showing no...
  • Siddharth: Is there any way to move the position of the docking controls (or eliminate them through some...
  • Chris: Yes, it is a challenge to organize functions into categories, especially with so many functions. Ken and I...
  • Mike: Thanks for fielding that one, Yair.
  • Yair Altman: Jimmy - if you mean that you wish to include hyperlinks in your function’s help comment, that will...
  • OysterEngineer: Thanks for explaining the Function Browser. I fired it up and gave it a try. It appears that it has...
  • Jimmy: Is there any way to include hyperlinks in a comment, such as the standard help at the beginning of a function?

These postings are the author's and don't necessarily represent the opinions of The MathWorks.