MATLAB: How to insert an image in the 2-D and 3-D plots in MATLAB 8.2 (R2013b)

imageimagescMATLABplotsurf

I want to display an image inside a 2-D plot and a 3-D plot. How can I do it?
 

Best Answer

For 2-D plots:
You can use the function IMAGE. 
The following example plots an image at the center of a scatter plot:
 
img = imread('peppers.png'); % Load a sample image

scatter(rand(1,20)-0.5,rand(1,20)-0.5); % Plot some random data
hold on; % Add to the plot

image([-0.1 0.1],[0.1 -0.1],img); % Plot the image
 
 
For 3-D plots:
The IMAGE function is no longer appropriate. 
In this case you will have to create a 3-D surface using the SURF function and texture map the image onto it. 
The following example shows how to put an image in the middle of a 3-D scatter plot:
 
[xSphere,ySphere,zSphere] = sphere(16); % Points on a sphere
scatter3(xSphere(:),ySphere(:),zSphere(:),'.'); % Plot the points
axis equal; % Make the axes scales match
hold on; % Add to the plot
xlabel('x');
ylabel('y');
zlabel('z');
img = imread('peppers.png'); % Load a sample image
xImage = [-0.5 0.5; -0.5 0.5]; % The x data for the image corners
yImage = [0 0; 0 0]; % The y data for the image corners
zImage = [0.5 0.5; -0.5 -0.5]; % The z data for the image corners
surf(xImage,yImage,zImage,... % Plot the surface
'CData',img,...
'FaceColor','texturemap');