image thumbnail

thumbnailScatter.m puts thumbnails over datapoints for better visualization


Derya (view profile)


05 Mar 2013 (Updated )

Helps you visualize patterns in your data.

% thumbnailScatter.m
% MATLAB script that plots thumbnails over datapoints
% Derya Akkaynak
% Massachusetts Institute of Technology
% March 4, 2013
% for questions/comments/bugs:

function thumbnailScatter(dataX,dataY,thumbnailPath)

% dataX is a vector of size 1xN (or Nx1)
% dataY is a vector of size 1xN (or Nx1)
% thumbnailPath is where the thumbnails that will be overlaid onto the data
% poits are stored.
% the script expects the thumbnails to be named thumb1.jpg, thumb2.jpg, but
% you can change the names as needed. The only requirement is that the thumbnails
% align with the data points. For example, [dataX(5),dataY(5)] is
% represented by thumb5.jpg.
% You should also provide a mask for the alphamap of your images (if
% needed). These should be from files named, e.g., 'mask5.mat'. Or change as
% needed.

sX = numel(dataX);
sY = numel(dataY);
assert(sX==sY,'There must be the same number of points in both input vectors');

% this part draws the main plot underneath. change dimensions as needed.
mainFig = figure;
set(mainFig,'position',[100 600 1000 1000]) % change these dimensions as needed
set(mainFig,'resize','off') % if resized, the images don't sit on the correct coords
hold on
xlabel('Test Data XYZ','fontsize',20)
ylabel('Change of test Data XYZ','fontsize',20)

mainAxes = gca; % this is the handle to the plot

curPos = get(mainAxes,'position');
xlims = get(mainAxes,'xlim');
ylims = get(mainAxes,'ylim');

sx = 0.08; % size of the "image" to be overlaid, this can be changed
sy = 0.08;

xoffset = sx./3; % offsets to place the axes over the point exactly
yoffset = sy./3; % these may be changed if image is resized

for i = 1:sX
    loc = [dataX(i) dataY(i)];
    % read the thumbnail to be plotted over the data point
    img = imread([thumbnailPath,'/thumb',num2str(i),'.jpg']); % change this as needed
    sImg = size(img);
    % read the mask that will cut the excess/unwanted area in the img
    % if the mask is the same for all images, this may be taken outside the
    % loop
    sMask = size(mask);
    if (sImg(1) ~=sMask(1)) || (sImg(2) ~=sMask(2))
        mask = imresize(mask,[sImg(1) sImg(2)]);
    pos = [curPos(1) + curPos(3) * (loc(1) - xlims(1)) / (xlims(2) - xlims(1)),...
        curPos(2) + curPos(4) * (loc(2) - ylims(1)) / (ylims(2) - ylims(1))];
    imgAxes = axes('position',[pos(1) - xoffset pos(2)- yoffset sx sy]);
    hold on
    himg = imshow(uint8(img),'Parent',imgAxes);
    set(himg, 'AlphaData', mask*1);  

Contact us