运行以下程序时,它变为相同的数字。 我想要相同的颜色编码。
x=-5:0.1:5;
y=-5:0.1:5;
[X,Y]=meshgrid(x,y);
z1=X.^2+Y.^2-25;
z2=X.^2+Y.^2-50;
clf();
f=gcf();
f.color_map=jetcolormap(32);
subplot(1,2,1);
contourf([],[],z1,32);
subplot(1,2,2);
contourf([],[],z2,32);
答案 0 :(得分:0)
我不知道如何使用contourf
,但如果您可以使用surf
,我有一个建议。
请注意,surf
会生成3D绘图,但您可以将其旋转为2D绘图。并且作为曲面,它具有cdata_mapping
属性,可以从scaled
更改为direct
并根据使用的实际范围缩放颜色矩阵(定义每个面的颜色) :
x=-5:0.1:5;
y=-5:0.1:5;
[X,Y]=meshgrid(x,y);
z1=X.^2+Y.^2-25;
z2=X.^2+Y.^2-50;
Zmin=min(min(z1),min(z2)); //Absolut minimum of all data sets
Zmax=max(max(z1),max(z2)); //Absolut maximum of all data sets
nc=100; //number of colors in colormap
clf();
f=gcf();
f.color_map=jetcolormap(nc);
subplot(1,2,1);
surf(X,Y,z1);
h=gce();
cm=h.data.color; //color matrix
h.data.color=((cm-Zmin)./(Zmax-Zmin)).*(nc-1)+1; //scale color matrix & convert to color number
h.cdata_mapping="direct"; //change color mapping from 'scaled' to 'direct'
h.color_mode=-1; //don't draw mesh
a=gca();
a.view="2d"; //2D view instead of 3D
colorbar(Zmin,Zmax);
subplot(1,2,2);
surf(X,Y,z2);
h=gce();
cm=h.data.color; //color matrix
h.data.color=((cm-Zmin)./(Zmax-Zmin)).*(nc-1)+1; //scale color matrix & convert to color number
h.cdata_mapping="direct"; //change color mapping from 'scaled' to 'direct'
h.color_mode=-1; //don't draw mesh
a=gca();
a.view="2d"; //2D view instead of 3D
colorbar(Zmin,Zmax);
这是一个适合您的解决方案,还是在任何情况下都必须使用contourf
?