我有一个二维散点图,在原点位置想要显示一张图片(不是一个彩色的正方形,而是一张实际的图片)。有没有办法做到这一点?
我还将绘制一个三维球体,在原点位置也希望显示一张图片。
IMAGE 函数是您要寻找的。这里有一个例子:
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
使用IMAGE函数已经不再适用,因为除非从正上方(即沿正z轴方向)查看轴,否则图像将无法显示。在这种情况下,您需要使用SURF函数创建一个三维曲面,并将图像映射到其上。以下是一个示例:
[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');
请注意,这个表面在空间中是固定的,因此当您旋转轴时,图像不会始终直接面向相机。如果您希望贴有纹理的表面自动旋转,使其始终垂直于相机的视线,那么这将是一个更加复杂的过程。