问题
When the following program is run, It becomes the same figure. I want the same color-coding.
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);
回答1:
I don't know how to do it with contourf
, but if you can use surf
instead, I have a suggestion.
Please note, that surf
generates a 3D plot, but you can rotate it to seem as a 2D plot. And as a surface it has the cdata_mapping
property, which can be changed from scaled
to direct
and scale the color matrix (which defines the color of each facet) according to the actual range used:
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);
Is this a solution for you, or you have to use contourf
in any circumstances?
来源:https://stackoverflow.com/questions/32880074/scilab-same-colormap-in-contourf