I have some data (a function of two parameters) stored in a matlab format, and I'd like to use matlab to plot it. Once I read the data in, I use mesh()
to make a plot. My mesh()
plot gives me the the value of the function as a color and a surface height, like this:
我有一些数据(两个参数的函数)存储在matlab格式中,我想用matlab绘图。一旦我读取了数据,我就使用mesh()来绘制一个图。mesh()绘图给出了函数作为颜色和表面高度的值,如下所示:
What matlab plotting function should I use to make a 2D mesh plot where the dependent variable is represented as only a color? I'm looking for something like pm3d map
in gnuplot.
我应该使用什么matlab绘图函数来绘制一个二维网格图,其中因变量仅表示为一种颜色?我在gnuplot寻找pm3d地图。
4 个解决方案
#1
37
By default mesh
will color surface values based on the (default) jet
colormap (i.e. hot is higher). You can additionally use surf
for filled surface patches and set the 'EdgeColor'
property to 'None'
(so the patch edges are non-visible).
默认情况下,mesh会根据(默认的)jet colormap(即hot更高)给表面值上色。您还可以对填充的表面贴片使用surf,并将“EdgeColor”属性设置为“None”(因此贴片边缘是不可见的)。
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
% surface in 3D
figure;
surf(Z,'EdgeColor','None');
2D map: You can get a 2D map by switching the view
property of the figure
2D地图:你可以通过切换图形的视图属性得到2D地图。
% 2D map using view
figure;
surf(Z,'EdgeColor','None');
view(2);
... or treating the values in Z
as a matrix, viewing it as a scaled image using imagesc
and selecting an appropriate colormap.
…或者将Z中的值作为一个矩阵来处理,将其视为使用imagesc的缩放图像,并选择合适的colormap。
% using imagesc to view just Z
figure;
imagesc(Z);
colormap jet;
The color pallet of the map is controlled by colormap(map)
, where map
can be custom or any of the built-in colormaps provided by MATLAB:
map的颜色托盘由colormap(map)控制,其中map可以定制,也可以使用MATLAB提供的任何内置colormaps:
Update/Refining the map: Several design options on the map (resolution, smoothing, axis etc.) can be controlled by the regular MATLAB options. As @Floris points out, here is a smoothed, equal-axis, no-axis labels maps, adapted to this example:
更新/细化地图:地图上的几个设计选项(分辨率、平滑度、轴等)可以由常规的MATLAB选项控制。正如@Floris指出的,这里有一个平滑的、等轴的、无轴的标签映射,适用于这个示例:
figure;
surf(X, Y, Z,'EdgeColor', 'None', 'facecolor', 'interp');
view(2);
axis equal;
axis off;
#2
19
gevang's answer is great. There's another way as well to do this directly by using pcolor
. Code:
gevang的回答是伟大的。还有一种直接使用pcolor的方法。代码:
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure;
subplot(1,3,1);
pcolor(X,Y,Z);
subplot(1,3,2);
pcolor(X,Y,Z); shading flat;
subplot(1,3,3);
pcolor(X,Y,Z); shading interp;
Output:
输出:
Also, pcolor
is flat too, as show here (pcolor
is the 2d base; the 3d figure above it is generated using mesh
):
同样,pcolor也是平的,如图所示(pcolor是2d base;上面的3d图形是用mesh生成的):
#3
4
Note that both pcolor and "surf + view(2)" do not show the last row and the last column of your 2D data.
请注意,pcolor和“surf + view(2)”都没有显示您的2D数据的最后一行和最后一列。
On the other hand, using imagesc, you have to be careful with the axes. The surf and the imagesc examples in gevang's answer only (almost -- apart from the last row and column) correspond to each other because the 2D sinc function is symmetric.
另一方面,使用imagesc,你必须小心使用坐标轴。在gevang的回答中,surf和imagesc的例子(几乎——除了最后一行和列之外)是相互对应的,因为二维sinc函数是对称的。
To illustrate these 2 points, I produced the figure below with the following code:
为了说明这两点,我用下面的代码生成了如下图:
[x, y] = meshgrid(1:10,1:5);
z = x.^3 + y.^3;
subplot(3,1,1)
imagesc(flipud(z)), axis equal tight, colorbar
set(gca, 'YTick', 1:5, 'YTickLabel', 5:-1:1);
title('imagesc')
subplot(3,1,2)
surf(x,y,z,'EdgeColor','None'), view(2), axis equal tight, colorbar
title('surf with view(2)')
subplot(3,1,3)
imagesc(flipud(z)), axis equal tight, colorbar
axis([0.5 9.5 1.5 5.5])
set(gca, 'YTick', 1:5, 'YTickLabel', 5:-1:1);
title('imagesc cropped')
colormap jet
As you can see the 10th row and 5th column are missing in the surf plot. (You can also see this in images in the other answers.)
正如你所看到的,第10行和第5列在surf中消失了。(你也可以在其他答案的图片中看到。)
Note how you can use the "set(gca, 'YTick'..." (and Xtick) command to set the x and y tick labels properly if x and y are not 1:1:N.
注意如何使用“set(gca, 'YTick'…)”(和Xtick)命令,如果x和y不是1:1:N,则正确地设置x和y标记。
Also note that imagesc only makes sense if your z data correspond to xs and ys are (each) equally spaced. If not you can use surf (and possibly duplicate the last column and row and one more "(end,end)" value -- although that's a kind of a dirty approach).
还要注意,如果z数据与xs和ys(每个)相等,那么imagesc仅是有意义的。如果没有,您可以使用surf(并且可能复制最后的列和行以及一个“(end,end)”值——尽管这是一种很脏的方法)。
#4
2
I also suggest using contourf(Z)
. For my problem, I wanted to visualize a 3D histogram in 2D, but the contours were too smooth to represent a top view of histogram bars.
我也建议使用contourf(Z)。对于我的问题,我想要在2D中可视化一个3D直方图,但是轮廓太光滑了,不能代表直方图条的俯视图。
So in my case, I prefer to use jucestain's answer. The default shading faceted
of pcolor()
is more suitable. However, pcolor()
does not use the last row and column of the plotted matrix. For this, I used the padarray()
function:
所以在我的例子中,我更喜欢用jucestain的答案。pcolor()的默认阴影面更合适。但是,pcolor()不使用绘制矩阵的最后一行和列。为此,我使用了padarray()函数:
pcolor(padarray(Z,[1 1],0,'post'))
Sorry if that is not really related to the original post
不好意思,如果这和最初的帖子没有什么关系
#1
37
By default mesh
will color surface values based on the (default) jet
colormap (i.e. hot is higher). You can additionally use surf
for filled surface patches and set the 'EdgeColor'
property to 'None'
(so the patch edges are non-visible).
默认情况下,mesh会根据(默认的)jet colormap(即hot更高)给表面值上色。您还可以对填充的表面贴片使用surf,并将“EdgeColor”属性设置为“None”(因此贴片边缘是不可见的)。
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
% surface in 3D
figure;
surf(Z,'EdgeColor','None');
2D map: You can get a 2D map by switching the view
property of the figure
2D地图:你可以通过切换图形的视图属性得到2D地图。
% 2D map using view
figure;
surf(Z,'EdgeColor','None');
view(2);
... or treating the values in Z
as a matrix, viewing it as a scaled image using imagesc
and selecting an appropriate colormap.
…或者将Z中的值作为一个矩阵来处理,将其视为使用imagesc的缩放图像,并选择合适的colormap。
% using imagesc to view just Z
figure;
imagesc(Z);
colormap jet;
The color pallet of the map is controlled by colormap(map)
, where map
can be custom or any of the built-in colormaps provided by MATLAB:
map的颜色托盘由colormap(map)控制,其中map可以定制,也可以使用MATLAB提供的任何内置colormaps:
Update/Refining the map: Several design options on the map (resolution, smoothing, axis etc.) can be controlled by the regular MATLAB options. As @Floris points out, here is a smoothed, equal-axis, no-axis labels maps, adapted to this example:
更新/细化地图:地图上的几个设计选项(分辨率、平滑度、轴等)可以由常规的MATLAB选项控制。正如@Floris指出的,这里有一个平滑的、等轴的、无轴的标签映射,适用于这个示例:
figure;
surf(X, Y, Z,'EdgeColor', 'None', 'facecolor', 'interp');
view(2);
axis equal;
axis off;
#2
19
gevang's answer is great. There's another way as well to do this directly by using pcolor
. Code:
gevang的回答是伟大的。还有一种直接使用pcolor的方法。代码:
[X,Y] = meshgrid(-8:.5:8);
R = sqrt(X.^2 + Y.^2) + eps;
Z = sin(R)./R;
figure;
subplot(1,3,1);
pcolor(X,Y,Z);
subplot(1,3,2);
pcolor(X,Y,Z); shading flat;
subplot(1,3,3);
pcolor(X,Y,Z); shading interp;
Output:
输出:
Also, pcolor
is flat too, as show here (pcolor
is the 2d base; the 3d figure above it is generated using mesh
):
同样,pcolor也是平的,如图所示(pcolor是2d base;上面的3d图形是用mesh生成的):
#3
4
Note that both pcolor and "surf + view(2)" do not show the last row and the last column of your 2D data.
请注意,pcolor和“surf + view(2)”都没有显示您的2D数据的最后一行和最后一列。
On the other hand, using imagesc, you have to be careful with the axes. The surf and the imagesc examples in gevang's answer only (almost -- apart from the last row and column) correspond to each other because the 2D sinc function is symmetric.
另一方面,使用imagesc,你必须小心使用坐标轴。在gevang的回答中,surf和imagesc的例子(几乎——除了最后一行和列之外)是相互对应的,因为二维sinc函数是对称的。
To illustrate these 2 points, I produced the figure below with the following code:
为了说明这两点,我用下面的代码生成了如下图:
[x, y] = meshgrid(1:10,1:5);
z = x.^3 + y.^3;
subplot(3,1,1)
imagesc(flipud(z)), axis equal tight, colorbar
set(gca, 'YTick', 1:5, 'YTickLabel', 5:-1:1);
title('imagesc')
subplot(3,1,2)
surf(x,y,z,'EdgeColor','None'), view(2), axis equal tight, colorbar
title('surf with view(2)')
subplot(3,1,3)
imagesc(flipud(z)), axis equal tight, colorbar
axis([0.5 9.5 1.5 5.5])
set(gca, 'YTick', 1:5, 'YTickLabel', 5:-1:1);
title('imagesc cropped')
colormap jet
As you can see the 10th row and 5th column are missing in the surf plot. (You can also see this in images in the other answers.)
正如你所看到的,第10行和第5列在surf中消失了。(你也可以在其他答案的图片中看到。)
Note how you can use the "set(gca, 'YTick'..." (and Xtick) command to set the x and y tick labels properly if x and y are not 1:1:N.
注意如何使用“set(gca, 'YTick'…)”(和Xtick)命令,如果x和y不是1:1:N,则正确地设置x和y标记。
Also note that imagesc only makes sense if your z data correspond to xs and ys are (each) equally spaced. If not you can use surf (and possibly duplicate the last column and row and one more "(end,end)" value -- although that's a kind of a dirty approach).
还要注意,如果z数据与xs和ys(每个)相等,那么imagesc仅是有意义的。如果没有,您可以使用surf(并且可能复制最后的列和行以及一个“(end,end)”值——尽管这是一种很脏的方法)。
#4
2
I also suggest using contourf(Z)
. For my problem, I wanted to visualize a 3D histogram in 2D, but the contours were too smooth to represent a top view of histogram bars.
我也建议使用contourf(Z)。对于我的问题,我想要在2D中可视化一个3D直方图,但是轮廓太光滑了,不能代表直方图条的俯视图。
So in my case, I prefer to use jucestain's answer. The default shading faceted
of pcolor()
is more suitable. However, pcolor()
does not use the last row and column of the plotted matrix. For this, I used the padarray()
function:
所以在我的例子中,我更喜欢用jucestain的答案。pcolor()的默认阴影面更合适。但是,pcolor()不使用绘制矩阵的最后一行和列。为此,我使用了padarray()函数:
pcolor(padarray(Z,[1 1],0,'post'))
Sorry if that is not really related to the original post
不好意思,如果这和最初的帖子没有什么关系