关于matlab中用griddata绘制伪色彩图的一点经验教训

整个事情起源于这个图

Chinese Physics Letters, 2017(8):117-120.

    这是之前和别人组之前合作测试时候我师兄画的,横轴压力纵轴温度,颜色轴是电阻率的对数,很直观的看出来了金属到半导体绝缘体的转变与压力温度的关系。

    还记得那是一个阳光明媚的能热死个人的下午,我去找老板讨论这一组数据,大概意思就是压力下这个样品从一个绝缘体/半导体转变为一个金属。老板拿出了师兄之前画的那个图,说你去试试也画下类似这个的图吧,应该不难的。

一组打了厚码是数据

    “应该不难的”这种话从老板嘴里说出来一般都不可信,然后我去问师兄他比较忙只告诉我他用matlab画的伪色彩图,那怎么办呢,我只能一天从零开始入门matlab了啊。

    先说说我摸索的过程,我思路一开始是这样的:我们测试是在不同压力下对其电阻-温度关系进行的测试,而要将其转化为三维图像首先要将其转化为矩阵,而不同压力下测试的温度范围都不同,也就意味着没法同时放到矩阵里,那该怎么办呢?

    我选择了igor pro中的插值功能来解决这个问题,自己写了个小的macro来执行。

macro interpof(T1,T2,pressure)

variable T1=3.5, T2=290, pressure

Make/N=600/D T_tot

SetScale/I x,T1,T2,T_tot

T_tot=x

String newname1="T_R_"+num2str(pressure)+"G"

String newname2="R_"+num2str(pressure)+"G"

String newname3="R_"+num2str(pressure)+"G_tot"

Duplicate T_tot  $newname3

$newname3=interp(T_tot,$newname1,$newname2)

Duplicate $newname3 ::$newname3

end

大概意思就是根据输入的T1、T2两个温度点 ,在其中均匀分600个位置,然后在这600个位置中对数据结果进行插值,因为我们的测试一般会得到几千个点,所以这样的简单插值精度完全可以保证。分别对不同压力下数据允许这个macro,即可得到同一温度下不同压力对应的电阻值。将不同压力下插值得到的600*n个数据在excel中排好导入matlab即可得到矩阵。运行griddata指令即可得到结果。

[p,t,z]=griddata(P,T,lnR,linspace(2,12)',linspace(3.5,290),'v4');%插值

pcolor(p,t,z);shading interp


matlab插值得到的结果
把相变点(被马赛克掉的)这些结果加上之后的结果

    把相变点一加坐标轴一标也是有那么点意思,不过有个小问题,对每个压力点的测试温度范围都不完全相同,图中是对3.8-290K进行的插值,但许多时候测量范围会差很多,尤其是对一些高温超导体,其下半部分不可能这么整齐,比如下图,而此处使用这种办法只能在矩阵中手动将没有数字的地方设为0,非常麻烦;同时也不能使用'v4'参数,而要换为cubic spline模式。

Nature Communications, 2018, 9(380).

    然后我又去找师兄讨论了,妄图敲诈出他的原始方法,经过一番讨论,以及我自己的摸索,终于找到了更一般的方法。

    我们首先来看看matlab中griddata的描述

griddata

Interpolates scattered data - generally to produce gridded data

    Vq = griddata(X,Y,V,Xq,Yq) fits a surface of the form V = F(X,Y) to the     scattered data in (X, Y, V). The coordinates of the data points are     defined by the vectors (X,Y) and V defines the corresponding values.     griddata interpolates the surface F at the query points (Xq,Yq) and     returns the values in Vq. The query points (Xq, Yq) generally represent     a grid obtained from NDGRID or MESHGRID, hence the name griddata.

    Vq = griddata(X,Y,Z,V,Xq,Yq,Zq) fits a hyper-surface of the form     V = F(X,Y,Z) to the scattered data in (X, Y, Z, V). The coordinates of     the data points are defined by the vectors (X,Y,Z) and V defines the     corresponding values. griddata interpolates the surface F at the query     points (Xq,Yq,Zq) and returns the values in Vq.

      Vq = griddata(X,Y,V, xq, yq) where xq is a row vector and yq is a     column vector, expands (xq, yq) via [Xq, Yq] = meshgrid(xq,yq).     [Xq, Yq, Vq] = griddata(X,Y,V, xq, yq) returns the grid coordinates     arrays in addition.

    Note: The syntax for implicit meshgrid expansion of (xq, yq) will be     removed in a future release.

    griddata(..., METHOD) where METHOD is one of

        'nearest'  - Nearest neighbor interpolation

        'linear'    - Linear interpolation (default)

        'natural'  - Natural neighbor interpolation

        'cubic'    - Cubic interpolation (2D only)

        'v4'        - MATLAB 4 griddata method (2D only)

    defines the interpolation method. The 'nearest' and 'linear' methods     have discontinuities in the zero-th and first derivatives respectively,     while the 'cubic' and 'v4' methods produce smooth surfaces.  All the     methods except 'v4' are based on a Delaunay triangulation of the data.

    Example 1:

      % Interpolate a 2D scattered data set over a uniform grid

      x = 4*rand(100,1)-2; y = 8*rand(100,1)-4; z = x.*exp(-x.^2-y.^2);

      [xq,yq] = meshgrid(-2:.2:2, -4:.4:4);

      zq = griddata(x,y,z,xq,yq);

      mesh(xq,yq,zq), hold on, plot3(x,y,z,'o'), hold off

    Example 2:

      % Interpolate a 3D data set over a grid in the x-y (z=0) plane

      x = 2*rand(5000,1)-1;

y = 2*rand(5000,1)-1;

z = 2*rand(5000,1)-1;

      v = x.^2 + y.^2 + z.^2;

      d = -0.8:0.05:0.8;

      [xq,yq,zq] = meshgrid(d,d,0);

      vq = griddata(x,y,z,v,xq,yq,zq);

      surf(xq,yq,vq);

        不仅可以处理矩阵的数据,同时也可对输入的(x,y,z)点集进行插值,此方法即不再将其视为矩阵,而视为xy对应的二元函数z,输入所有的xyz数据进行插值即可。然而处理时首先还要注意点的数量,我们测试的原始数据加起来总共有上万个点,为了提高效率依旧可以先使用igor pro对数据插值及平滑处理,不过不用再设定温度范围,直接选择点的个数(600)插值即可,然后将得到的所有数据(600*n)输入excel中分别为三列:压力、温度、电阻,导入matlab中作为三个向量,运行指令

[x,y,z]=griddata(P,T,Z,linspace(2,12)',linspace(0,300),'cubic');%插值

pcolor(x,y,z);shading interp

即可得到完整的包括0K附近及300K附近边角缺憾的相图。之后再添加其他说明数据即可。

这种方法相比于前者更为简便,且应用范围更广,我以后应该不会用我费老大劲搞出来的笨办法了。

你可能感兴趣的:(关于matlab中用griddata绘制伪色彩图的一点经验教训)