是否可以控制轮廓中的颜色?

时间:2016-08-10 04:21:25

标签: matlab plot matlab-figure figure contourf

enter image description here下午好, 我有这个代码,我想指定三个彩色区域。一种是当最终函数C和C1合并在一起时如代码所示小于0,介于0和1之间,当大于1.在代码中我需要合并的contourf图最后合并和定义在三个简单的地区。结果如下图所示。您可能会首先想到,最主要的颜色是亮蓝色但是当您查看contourf图和颜色条时它们由于某种原因不匹配。谢谢您的帮助。向管理员道歉,因为没有将此问题作为单独的问题发布。

    [r,R] = meshgrid(0.1:0.01:10,-5:0.01:5);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Z=(((R).^2)-1 );
           A=2+ (R.*(r+(1./r)));
           B=sqrt(Z).*(r-(1./r));
           Ratio=acoth(A./B);
           D= (r+(1./r))./4;
           E=D./sqrt(Z);

           C=D.*E.*Ratio;
    C(R==1)=(r(R==1)+(1./r(R==1)))*0.25.*((r(R==1).^2-1)./(2*r(R==1)+r(R==1).^2-1) );
    C(R==-1)=(r(R==-1)+(1./r(R==-1)))*0.25.*((r(R==1).^2-1)./(2*r(R==1)-r(R==1).^2+1) );
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Z1=((1 -(R).^2));
           A1=2+ (R.*(r+(1./r)));
           B1=sqrt(Z1).*(r-(1./r));
           Ratio1=EA(A1./B1);
           D1= (r+(1./r))./4;
           E1=D1./sqrt(Z1);

           C1=D1.*E1.*Ratio1;
    C1(R==1)=(r(R==1)+(1./r(R==1)))*0.25.*((r(R==1).^2-1)./(2*r(R==1)+r(R==1).^2-1) );
    C1(R==-1)=(r(R==-1)+(1./r(R==-1)))*0.25.*((r(R==1).^2-1)./(2*r(R==1)-r(R==1).^2+1) );

    test=C;
    test((R<1)&(R>-1))=nan;   
    test1=C1;
    test1(R>1)=nan;
    test2=test1;
    test2(R<-1)=nan;

       figure
          contourf(r,R,test)
        hold on
         contourf(r,R,test2);
        colormap(jet(3))
        conts = [-1:1:2];
            h=colorbar;
         set(get(h,'ylabel'),'string','\gamma P_P L','FontSize',18)
           xlabel('$r$','Interpreter','latex','FontSize',18)
        ylabel('$D \over 2\sqrt{M}$','Interpreter','latex','FontSize',18)
    set(h,'YTick',conts)
    set(gca,'fontsize',18)
        hold off 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  y  = EA(x)

 y = acot(x);

y(y<0)=y(y<0)+pi;

return


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Outcome of the code

1 个答案:

答案 0 :(得分:1)

好的,代码有两个主要问题。

1)需要将colorbar设置为正确的限制。这意味着,您需要将xolorbar色标设置为另一个值。颜色条的颜色限制目前是名为'Clim'的属性,位于2015b的轴中。这些事情可能会改变,因此我建议使用函数caxis。由于你有3种颜色并且想要范围[-inf - &gt; 0,0 - &gt; 1,1 - &gt;天道酬勤。我建议使用限制caxis([-1,2]);这会在您需要的位置设置中断。

2)MATLAB自动设置级别(MATLAB称之为图中的拓扑级别)。这些值与您想要的不符。使用参数contourf(r,R,test, 'ShowText', 'on'),您将看到我的意思。我不确定是否有内置函数来修改它们,但该属性被称为LevelList并且可以在等高线图中找到。您可以在contourf函数

中使用以下内容
contourf(r,R,test, 'LevelList', [-inf,-1,0,1,inf]);
contourf(r,R,test, 'LevelList', [-inf,-1,0,1,inf]);