How do I insert an image in my 2-D and 3-D plots in MATLAB 8.2 (R2013b)?
26 views (last 30 days)
Show older comments
MathWorks Support Team
on 25 Aug 2016
Answered: MathWorks Support Team
on 25 Aug 2016
I want to display an image inside a 2-D plot and a 3-D plot. How can I do it?
Accepted Answer
MathWorks Support Team
on 29 Mar 2018
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');
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!