Bill Cheatham
Bill Cheatham

Reputation: 11917

Plot images as axis labels in MATLAB

I am plotting a 7x7 pixel 'image' in MATLAB, using the imagesc command:

imagesc(conf_matrix, [0 1]);

This represents a confusion matrix, between seven different objects. I have a thumbnail picture of each of the seven objects that I would like to use as the axes tick labels. Is there an easy way to do this?

Upvotes: 2

Views: 4122

Answers (2)

Bill Cheatham
Bill Cheatham

Reputation: 11917

@Itmar Katz gives a solution very close to what I want to do, which I've marked as 'accepted'. In the meantime, I made this dirty solution using subplots, which I've given here for completeness. It only works up to a certain size input matrix though, and only displays well when the figure is square.


conf_mat = randn(5);
A = imread('peppers.png');
tick_images = {A, A, A, A, A};

n = length(conf_mat) + 1;

% plotting axis labels at left and top
for i = 1:(n-1)
    subplot(n, n, i + 1); 
    imshow(tick_images{i});
    subplot(n, n, i * n + 1);
    imshow(tick_images{i});
end

% generating logical array for where the confusion matrix should be
idx = 1:(n*n);
idx(1:n) = 0;
idx(mod(idx, n)==1) = 0;

% plotting the confusion matrix
subplot(n, n, find(idx~=0));
imshow(conf_mat);
axis image
colormap(gray)

enter image description here

Upvotes: 3

Itamar Katz
Itamar Katz

Reputation: 9645

I don't know an easy way. The axes properties XtickLabel which determines the labels, can only be strings.

If you want a not-so-easy way, you could do something in the spirit of the following non-complete (in the sense of a non-complete solution) code, creating one label:

h = imagesc(rand(7,7));
axh = gca;
figh = gcf;
xticks = get(gca,'xtick');
yticks = get(gca,'ytick');
set(gca,'XTickLabel','');
set(gca,'YTickLabel','');
pos = get(axh,'position'); % position of current axes in parent figure

pic = imread('coins.png');
x = pos(1);
y = pos(2);
dlta = (pos(3)-pos(1)) / length(xticks); % square size in units of parant figure

% create image label
lblAx = axes('parent',figh,'position',[x+dlta/4,y-dlta/2,dlta/2,dlta/2]);
imagesc(pic,'parent',lblAx)
axis(lblAx,'off')

One problem is that the label will have the same colormap of the original image.

Upvotes: 3

Related Questions