填充 pgfplots 中线上下的区域

填充 pgfplots 中线上下的区域

在下面的代码中,当我尝试执行软剪辑命令时\addplot [red, fill opacity=0.1] fill between[of=C and D,soft clip={domain=0:0.6336}],我的输出文件显示为空白,仅显示页码。有没有更简单的方法用红色填充第二个图中 100 以上和 -100 以下的区域,因此该图如下所示:

在此处输入图片描述

以下是代码:

\documentclass{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usetikzlibrary{pgfplots.groupplots, matrix}
\usepgfplotslibrary{fillbetween}
\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{float}
\usepackage{subfig}

\begin{filecontents*}{data23.csv}
A   B   C   D
0   -14.9000001 100 -100
0.0000064   8.83999991  100 -100
0.0000128   -3.73000002 100 -100
0.0000192   -2.80000019 100 -100
0.0000256   8.83999991  100 -100
0.000032    15.82999992 100 -100
0.0000384   8.37999988  100 -100
0.0000448   -1.4000001  100 -100
0.0000512   -6.99000001 100 -100
0.0000576   -11.6400001 100 -100
0.000064    -2.33000016 100 -100
0.0000704   0.4599998   100 -100
0.0000768   -1.4000001  100 -100
0.0000832   -19.10000014    100 -100
0.0000896   0   100 -100
0.000096    -4.19000006 100 -100
0.0001024   -15.84000015    100 -100
0.0001088   -5.13000011 100 -100
0.0001152   17.23000002 100 -100
0.0001216   7.44999981  100 -100
0.000128    10.24000001 100 -100
0.0001344   -2.33000016 100 -100
0.0001408   8.37999988  100 -100
0.0001472   -63.80000019    100 -100
0.0001536   -1851.47    100 -100
0.00016 -959001.16  100 -100
0.0001664   -959001.16  100 -100
0.0001728   -959001.16  100 -100
0.0001792   -959001.16  100 -100
0.0001856   -919131.57  100 -100
0.000192    194777.73   100 -100
0.0001984   238253.27   100 -100
0.0002048   277420.5    100 -100
0.0002112   291163.1    100 -100
0.0002176   286195.89   100 -100
0.000224    255122.31   100 -100
0.0002304   182965.3    100 -100
0.0002368   74969.14    100 -100
0.0002432   1717.82 100 -100
0.0002496   -46980.57   100 -100
0.000256    -60135.04   100 -100
0.0002624   -87181.11   100 -100
0.0002688   -82944.99   100 -100
0.0002752   -64264.06   100 -100
0.0002816   -42486.94   100 -100
0.000288    -19782.69   100 -100
0.0002944   -1171.61    100 -100
0.0003008   13164.71    100 -100
0.0003072   21098.18    100 -100
0.0003136   23432.54    100 -100
0.00032 22276.77    100 -100
0.0003264   18429.47    100 -100
0.0003328   11196.82    100 -100
0.0003392   4662.66 100 -100
0.0003456   -366.48 100 -100
0.000352    -3680.12    100 -100
0.0003584   -6535.09    100 -100
0.0003648   -7723.93    100 -100
0.0003712   -7477.13    100 -100
0.0003776   -6128.57    100 -100
0.000384    -3032.39    100 -100
0.0003904   -317.5800002    100 -100
0.0003968   248.1899998 100 -100
0.0004032   1216.77 100 -100
0.0004096   2771.61 100 -100
0.000416    3422.14 100 -100
0.0004224   1918.52 100 -100
0.0004288   947.6199999 100 -100
0.0004352   -420.96 100 -100
0.0004416   -2162.53    100 -100
0.000448    -1460.78    100 -100
0.0004544   153.6599999 100 -100
0.0004608   302.6799998 100 -100
0.0004672   605.8199999 100 -100
0.0004736   -415.8400002    100 -100
0.00048 -997.9200001    100 -100
0.0004864   -1122.25    100 -100
0.0004928   -926.2000001    100 -100
0.0004992   -723.6400001    100 -100
0.0005056   284.98  100 -100
0.000512    81.01999998 100 -100
0.0005184   572.29  100 -100
0.0005248   385.0999999 100 -100
0.0005312   -301.75 100 -100
0.0005376   -298.96 100 -100
0.000544    418.1599999 100 -100
0.0005504   71.7099998  100 -100
0.0005568   839.1199999 100 -100
0.0005632   1733.19 100 -100
0.0005696   1055.65 100 -100
0.000576    -544.3600001    100 -100
0.0005824   -648.2000001    100 -100
0.0005888   -1442.62    100 -100
0.0005952   -778.5900002    100 -100
0.0006016   398.1399999 100 -100
0.000608    1222.36 100 -100
0.0006144   1837.5  100 -100
0.0006208   -152.74 100 -100
0.0006272   -1656.83    100 -100
0.0006336   -477.77 100 -100
\end{filecontents*}

\pgfplotsset{%
    compat=1.12,
    every tick label/.append style={font=\scriptsize},
    every axis plot/.append style={line width=0.8pt},
    minor grid style={dashed,red},
    major grid style={dotted,green!50!black},
}

\captionsetup[subfigure]{labelfont={color=blue,scriptsize,it,bf},textfont={color=blue,scriptsize,it,bf},labelformat=parens,labelsep=space}

\begin{document}

\begin{figure}[H]
\begin{center}
\begin{tikzpicture}
\setcaptionsubtype
\begin{groupplot}[%
            ,group style={%
                ,group name=my plots
                ,group size=1 by 2
                ,vertical sep=2cm,
                ,horizontal sep = 2cm,
                ,ylabels at=edge left
            }
            ,width=10cm
            ,height=6cm
            ,try min ticks=5
            ,xlabel={\bfseries{\emph{\footnotesize{Number of Recursions}}}}
            ,grid=both
            ,every major grid/.style={gray, opacity=0.5}
            ]
\nextgroupplot[xmin = 0, xmax = 0.7]%
\addplot [blue,mark options={scale=.65}]table[x index=0,y index=1, x expr=\thisrow{A}*1000, col sep=space] {data23.csv};\label{plots:ltone}

\nextgroupplot[ymax = 500, ymin = -500, xmin = 0, xmax = 0.7]%
\addplot [blue,mark options={scale=.65}]table[x index=0,y index=1, x expr=\thisrow{A}*1000, col sep=space,restrict y to domain=-10000:10000] {data23.csv};\label{plots:lttwo}

\addplot [smooth,red,thick,name path=A] table[x index=0,y index=2, x expr=\thisrow{A}*1000, col sep=space]{data23.csv};
\addplot [draw=none,name path=B, domain=0:.6336, mark=none] {500};
%\addplot [red, fill opacity=0.1] fill between[of=A and B,soft clip={domain=0:0.6336}];

\addplot [smooth,red,thick,name path=C] table[x index=0,y index=3, x expr=\thisrow{A}*1000, col sep=space]{data23.csv};
\addplot [draw=none,name path=D, domain=0:.6336, mark=none] {-500};
%\addplot [red, fill opacity=0.1] fill between[of=C and D,soft clip={domain=0:0.6336}];

\end{groupplot}

\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r1.north) {\caption[]{Test 1\label{subplot:ltone}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r2.north) {\caption[]{Test 2\label{subplot:lttwo}}};

\end{tikzpicture}
\caption[]{Plot showing position ${\mathbf{P_{T}}}$}\label{abserror}
\end{center}
\end{figure}

\end{document}

答案1

有一些fill between问题groupplots例如pgfplots 中的 fillbetween 在 groupplots 中不起作用,或许还存在一些问题。

作为一种解决方法,您可以使用与如何根据轴的值突出显示 Pgfplots 图中的区域?,即添加 \usetikzlibrary{backgrounds},并添加

\begin{scope}[on background layer]
\fill [red!20] (0,100) rectangle (0.7,500);
\fill [red!20] (0,-100) rectangle (0.7,-500);
\end{scope}

axis环境中:

在此处输入图片描述

\documentclass{book}
\usepackage[top=3cm,bottom=3cm,left=3.2cm,right=3.2cm,headsep=10pt,a4paper]{geometry}
\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable, booktabs}
\usetikzlibrary{pgfplots.groupplots, matrix,backgrounds}
\usepgfplotslibrary{fillbetween}
\usepackage{filecontents}
\usepackage{graphicx}
\usepackage{float}
\usepackage{subfig}

\begin{filecontents*}{data23.csv}
A   B   C   D
0   -14.9000001 100 -100
0.0000064   8.83999991  100 -100
0.0000128   -3.73000002 100 -100
0.0000192   -2.80000019 100 -100
0.0000256   8.83999991  100 -100
0.000032    15.82999992 100 -100
0.0000384   8.37999988  100 -100
0.0000448   -1.4000001  100 -100
0.0000512   -6.99000001 100 -100
0.0000576   -11.6400001 100 -100
0.000064    -2.33000016 100 -100
0.0000704   0.4599998   100 -100
0.0000768   -1.4000001  100 -100
0.0000832   -19.10000014    100 -100
0.0000896   0   100 -100
0.000096    -4.19000006 100 -100
0.0001024   -15.84000015    100 -100
0.0001088   -5.13000011 100 -100
0.0001152   17.23000002 100 -100
0.0001216   7.44999981  100 -100
0.000128    10.24000001 100 -100
0.0001344   -2.33000016 100 -100
0.0001408   8.37999988  100 -100
0.0001472   -63.80000019    100 -100
0.0001536   -1851.47    100 -100
0.00016 -959001.16  100 -100
0.0001664   -959001.16  100 -100
0.0001728   -959001.16  100 -100
0.0001792   -959001.16  100 -100
0.0001856   -919131.57  100 -100
0.000192    194777.73   100 -100
0.0001984   238253.27   100 -100
0.0002048   277420.5    100 -100
0.0002112   291163.1    100 -100
0.0002176   286195.89   100 -100
0.000224    255122.31   100 -100
0.0002304   182965.3    100 -100
0.0002368   74969.14    100 -100
0.0002432   1717.82 100 -100
0.0002496   -46980.57   100 -100
0.000256    -60135.04   100 -100
0.0002624   -87181.11   100 -100
0.0002688   -82944.99   100 -100
0.0002752   -64264.06   100 -100
0.0002816   -42486.94   100 -100
0.000288    -19782.69   100 -100
0.0002944   -1171.61    100 -100
0.0003008   13164.71    100 -100
0.0003072   21098.18    100 -100
0.0003136   23432.54    100 -100
0.00032 22276.77    100 -100
0.0003264   18429.47    100 -100
0.0003328   11196.82    100 -100
0.0003392   4662.66 100 -100
0.0003456   -366.48 100 -100
0.000352    -3680.12    100 -100
0.0003584   -6535.09    100 -100
0.0003648   -7723.93    100 -100
0.0003712   -7477.13    100 -100
0.0003776   -6128.57    100 -100
0.000384    -3032.39    100 -100
0.0003904   -317.5800002    100 -100
0.0003968   248.1899998 100 -100
0.0004032   1216.77 100 -100
0.0004096   2771.61 100 -100
0.000416    3422.14 100 -100
0.0004224   1918.52 100 -100
0.0004288   947.6199999 100 -100
0.0004352   -420.96 100 -100
0.0004416   -2162.53    100 -100
0.000448    -1460.78    100 -100
0.0004544   153.6599999 100 -100
0.0004608   302.6799998 100 -100
0.0004672   605.8199999 100 -100
0.0004736   -415.8400002    100 -100
0.00048 -997.9200001    100 -100
0.0004864   -1122.25    100 -100
0.0004928   -926.2000001    100 -100
0.0004992   -723.6400001    100 -100
0.0005056   284.98  100 -100
0.000512    81.01999998 100 -100
0.0005184   572.29  100 -100
0.0005248   385.0999999 100 -100
0.0005312   -301.75 100 -100
0.0005376   -298.96 100 -100
0.000544    418.1599999 100 -100
0.0005504   71.7099998  100 -100
0.0005568   839.1199999 100 -100
0.0005632   1733.19 100 -100
0.0005696   1055.65 100 -100
0.000576    -544.3600001    100 -100
0.0005824   -648.2000001    100 -100
0.0005888   -1442.62    100 -100
0.0005952   -778.5900002    100 -100
0.0006016   398.1399999 100 -100
0.000608    1222.36 100 -100
0.0006144   1837.5  100 -100
0.0006208   -152.74 100 -100
0.0006272   -1656.83    100 -100
0.0006336   -477.77 100 -100
\end{filecontents*}

\pgfplotsset{%
    compat=1.12,
    every tick label/.append style={font=\scriptsize},
    every axis plot/.append style={line width=0.8pt},
    minor grid style={dashed,red},
    major grid style={dotted,green!50!black},
}

\captionsetup[subfigure]{labelfont={color=blue,scriptsize,it,bf},textfont={color=blue,scriptsize,it,bf},labelformat=parens,labelsep=space}

\begin{document}

\begin{figure}[H]
\begin{center}
\begin{tikzpicture}
\setcaptionsubtype
\begin{groupplot}[%
            ,group style={%
                ,group name=my plots
                ,group size=1 by 2
                ,vertical sep=2cm,
                ,horizontal sep = 2cm,
                ,ylabels at=edge left
            }
            ,width=10cm
            ,height=6cm
            ,try min ticks=5
            ,xlabel={\bfseries{\emph{\footnotesize{Number of Recursions}}}}
            ,grid=both
            ,every major grid/.style={gray, opacity=0.5}
            ]
\nextgroupplot[xmin = 0, xmax = 0.7]%
\addplot [blue,mark options={scale=.65}]table[x index=0,y index=1, x expr=\thisrow{A}*1000, col sep=space] {data23.csv};\label{plots:ltone}

\nextgroupplot[ymax = 500, ymin = -500, xmin = 0, xmax = 0.7]%
\addplot [blue,mark options={scale=.65}]table[x index=0,y index=1, x expr=\thisrow{A}*1000, col sep=space,restrict y to domain=-10000:10000] {data23.csv};\label{plots:lttwo}

\addplot [smooth,red,thick,name path=A] table[x index=0,y index=2, x expr=\thisrow{A}*1000, col sep=space]{data23.csv};
\addplot [draw=none,name path=B, domain=0:.6336, mark=none] {500};
%\addplot [red, fill opacity=0.1] fill between[of=A and B,soft clip={domain=0:0.6336}];

\begin{scope}[on background layer]
\fill [red!20] (0,100) rectangle (0.7,500);
\fill [red!20] (0,-100) rectangle (0.7,-500);
\end{scope}

\addplot [smooth,red,thick,name path=C] table[x index=0,y index=3, x expr=\thisrow{A}*1000, col sep=space]{data23.csv};
\addplot [draw=none,name path=D, domain=0:.6336, mark=none] {-500};


\end{groupplot}

\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r1.north) {\caption[]{Test 1\label{subplot:ltone}}};
\node[text width=.5\linewidth,align=center,anchor=south] at (my plots c1r2.north) {\caption[]{Test 2\label{subplot:lttwo}}};

\end{tikzpicture}
\caption[]{Plot showing position ${\mathbf{P_{T}}}$}\label{abserror}
\end{center}
\end{figure}

\end{document}

相关内容