pgfplots:如何模仿 MATLAB 填充轮廓图?

pgfplots:如何模仿 MATLAB 填充轮廓图?

下列的这个答案,我有一个数据文件P.dat,需要将其绘制为填充轮廓图。

但是我收到了这个错误

软件包 pgfplots 错误:严重:shader=interp:获得不支持的 pdf 着色类型“0”。这可能会损坏您的 pdf!。\end{axis}

如果我能知道错误来源以及如何使输出与 MATLAB 所需的输出相匹配,我将不胜感激。


数据

http://pastebin.com/PAvQMxdC


平均能量损失

\RequirePackage{luatex85}
\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}
    \addplot3[contour filled] table {P.dat};
    \end{axis}
    \end{tikzpicture}
\end{document}

MATLAB 期望输出

在此处输入图片描述


更新 1

我制作了另一个带有NaNz 值的数据文件来解释空白数据(空白),并指定了行数和列数,但我得到了这个不想要的输出。

P2文件

http://pastebin.com/CCUjeQUm

微波辐射计 2

\RequirePackage{luatex85}
\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=newest}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[view={0}{90}]
    \addplot3[contour filled,mesh/rows=31,mesh/cols=11,mesh/check=false] table {P2.dat};
    \end{axis}
    \end{tikzpicture}
\end{document}

输出 2

在此处输入图片描述


更新 2

回想一下我的原始 MATLAB 数据,我如何删除所有值z等于或超过的点1723以获得与我想要的输出相似的输出?

P3文件

http://pastebin.com/jvNVfJMU

微波辐射计 3

\RequirePackage{luatex85}
\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=newest}

\begin{document}
    \begin{tikzpicture}
    \begin{axis}[view={0}{90},colorbar, point meta max=1723, point meta min=300,]
    \addplot3[contour filled={number = 25,labels={false}},mesh/rows=31,mesh/cols=11,mesh/check=false
    ] table {P3.dat};
    \end{axis}
    \end{tikzpicture}
\end{document}

输出 3

在此处输入图片描述

答案1

这里我提出两种解决方案。


解决方案 1(图片左侧部分)

这尝试使用 PGFPlots 的功能重现 Matlab 图形。为了“确认”我做得对,我首先保存了你的Matlab 图像并裁剪轴部分。然后我将其添加为,\addplot graphics并在其上添加了真实图,即\addplot contour filled50% 透明度的图。这样就可以检查我是否找到了正确的间隔边界。

说我认为你上面的陈述是错误的。似乎你删除了所有值 >1600 的颜色。这也是有道理的,因为最大限度文件中的值为P3.dat1723...

解决方案 2(图片右侧部分)

这里我只是使用了上面裁剪的 Matlab 图像并重现了颜色条。

比较

正如您在解决方案 1 中看到的,有一些“伪影”使得结果不如 Matlab 结果那么平滑。这是因为轮廓计算/可视化仅有的取决于您的 PDF 查看器的功能。据说您的结果可能与我的不同。我从 Acrobat Reader XI 中的视图截取了屏幕截图。

这就是我赞成解决方案 2 的原因。

为了改善结果,你应该修改你的 Matlab 视图以仅有的显示轮廓,这意味着删除轴和网格线。那么唯一的区别可能是 Matlab 轮廓图中使用的/显示的颜色和 PGFPlots 计算的颜色条。具体来说,我的意思是一个可以使用 RGB 颜色,另一个可以使用 CMYK。但是既然你有插图画家,正如你所说,你可以检查一下,并调整其中一个部分,即 Matlab 或 PGFPlots 输出。

您还可以在 Matlab 中创建一个“纯”版本的颜色条,也就是说没有任何轴,并将此图形导入 PGFPlots 颜色条。当然,颜色完全相同的。


有关解决方案如何运作的更多详细信息,请查看代码中的注释。

% used PGFPlots v1.14
\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{
    % you need at least this `compat' level or higher to use the below features
    compat=1.14,
    % define a "white" colormap for the white part of the image
    colormap={no data}{
        color=(white)
%        color=(white)
        color=(red)
    },
    % load this colormap which is later used
    colormap/bluered,
    % define the "parula" colormap that was used to create the exported image
    % from Matlab
    % (borrowed from http://tex.stackexchange.com/a/336647/95441)
    colormap={parula}{
        rgb255=(53,42,135)
        rgb255=(15,92,221)
        rgb255=(18,125,216)
        rgb255=(7,156,207)
        rgb255=(21,177,180)
        rgb255=(89,189,140)
        rgb255=(165,190,107)
        rgb255=(225,185,82)
        rgb255=(252,206,46)
        rgb255=(249,251,14)
    },
}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            view={0}{90},
            colorbar,
            % modify the style of the colorbar a bit
            colorbar style={
                ytick distance=200,
                ymax=1600,
            },
            % this key--value is needed because of the `\addplot graphics'
            enlargelimits=false,
        ]
            % import the "exported" graphics
            \addplot graphics [
                xmin=0,
                xmax=50,
                ymin=0,
                ymax=150,
            ] {P3};
            % now try to reproduce the style of the exported graphics
            \addplot3 [
                % for that use, e.g., the `countour filled' feature ...
                contour filled={
                    % ... in combination with the `levels of colormap' feature
                    % which allows to customize the used colormap
                    levels from colormap={
                        % this part of the colormap is for the "colored" part
                        of colormap={
                            % here we use the above initialized `bluered' colormap
                            bluered,
%                            % (`viridis' is a colormap which is similar to the
%                            %  used `parula' comormap in Matlab.
%                            %  But because the yellow is hard to identify
%                            %  in this context we use the above colormap)
%                            viridis,
                            % with this we state there is more to come
                            target pos max=,
                            % and here we state where the corresponding levels
                            % should *start*
                            target pos={200,400,600,800,1000,1200,1400},
                        },
                        % here comes the second part of the colormap which
                        % should have no color which isn't possible or at least
                        % I don't have an idea how to do it ...
                        of colormap={
                            % ... so I use a "white" colormap instead
                            no data,
                            % here the lower end isn't needed because that was
                            % specified in the first part of the colormap
                            target pos min=,
                            % and here is the corresponding interval *start*
                            % for that colormap
                            % (as you can see -- or not ;) -- the white starts
                            %  at position/values >=1600)
                            target pos={1600},
                        },
                    },
                },
                % you need only to provide `rows' or `cols' because
                % PGFPlots can then calculate the other value together with
                % the provded number of data points
                mesh/rows=31,
                % make the plot half transparent to check that the `target pos'
                % of the colormap are chosen correct
                opacity=0.5,
            ] table {P3.dat};
        \end{axis}
    \end{tikzpicture}
    \begin{tikzpicture}
        \begin{axis}[
            % show the colorbar
            colorbar,
            % because there is no real plot where PGFPlots can get the `meta'
            % data from, one has to provide them manually
            point meta min=200,
            point meta max=1800,
            %%% here we define the needed colormap and its style again
            % we want to use constant intervals in the colormap
            colormap access=piecewise const,
            % and also here we have to provide the limits again ...
            of colormap/target pos min*=200,
            of colormap/target pos max*=1800,
            % ... and use this feature which makes easier to provide the
            % samples at the right position
            % (please have a look at the PGFPlots manual for more details)
            of colormap/sample for=const,
            % this is similar to the above example
            colormap={CM}{
                of colormap={
                    % ... except that we use the `parula' colormap here
                    parula,
                    target pos max=,
                    target pos={200,400,600,800,1000,1200,1400,1600},
                },
                of colormap={
                    no data,
                    target pos min=,
                    % here you can use an arbitrary value which is greater than
                    % the last `target pos' of the previous colormap part of course.
                    % But here I tried to "overwrite" the last color of the
                    % colormap, i.e. the "bright" yellow, as well by just
                    % giving it a very small interval
                    % (to show the effect increase the `ymax' value in the
                    %  `colorbar style')
                    target pos={1601},
                },
            },
            % modify the style of the colorbar a bit
            colorbar style={
                ytick distance=200,
                ymin=300,
                ymax=1600,
            },
            % this key--value is needed because of the `\addplot graphics'
            enlargelimits=false,
        ]
            \addplot graphics [
                xmin=0,
                xmax=50,
                ymin=0,
                ymax=150,
            ] {P3};
        \end{axis}
    \end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容