Labeling plots with images in Matlab

276 views Asked by At

Is there any way of labeling plots with images. For example, when I use the following:

plot(Y(:,1),Y(:,2),'o','LineWidth',2);

gname(names)

I can label each dot in a plot with a name. Is there any way to insert images instead of names?

1

There are 1 answers

0
A. Donda On BEST ANSWER

It is possible, but not as convenient as gname by far. You can use the low-level version of image to insert images in your plot at arbitrary positions. Here's a simple example which puts the "Mandrill" image that comes with Matlab with its upper left corner pixel at the position (pi/2, 0):

% example plot
x = linspace(0, 2*pi, 100);
plot(x, cos(x))

% insert image
load mandrill
colormap(map)
image('CData', X, 'XData', [pi/2, pi/2 + 0.5], 'YData', [0, -0.3])

The result looks like this:

image label

Problems with this approach:

  • There is no interactive point-and-click facility, you have to explicitly insert and position the image labels programmatically, or program such a point-and-click facility yourself. ginput might help doing so.

  • A figure window can only have one associated color map. That means if you have different images, they either all have to use the same colormap or have to be truecolor images.

  • Not just the position, but also the display size of the image has to be specified in the call to image, and both are by default specified with respect to the plot's coordinate system. This makes it hard to achieve the correct aspect ratio. You can switch (temporarily) to absolute units using the axes property 'Units' , but then you have to figure out the correct position in e.g. absolute millimeters or inches. Moreover, images are usually indexed with vertical coordinates increasing from top to bottom, while plots usually have vertical coordinates increasing from bottom to top. This is the reason for the negative value -0.3 in the 'YData' property above.

Alternatively, you can insert images each in their own little axes sitting on top of the plot's axes, which makes it easy to get the right orientation and aspect ratio using axis image. You'll still have the problem though to figure out the correct position for the axes.