如何从导入的数据中绘制 PGF 中的线条之间的阴影区域

如何从导入的数据中绘制 PGF 中的线条之间的阴影区域

我正在根据导入 PGF 的数据绘制两条线。然后我想为两条线之间的区域添加阴影,但我不知道该怎么做。我已尝试过name path = A本文中建议的等操作邮政,但是没有效果。

假设数据很简单,例如:

r           M       m   
0           2       2
0.010102    2.0202  2.0004
0.020204    2.0404  2.0005
0.030306    2.0606  2.0001
0.040408    2.0808  1.9993
0.05051     2.101   1.9981
0.060612    2.1212  1.9965
0.070714    2.1414  1.9945
0.080816    2.1616  1.9921
0.090918    2.1818  1.9893
0.10102     2.202   1.986
0.11112     2.2222  1.9824
0.12122     2.2424  1.9783
0.13133     2.2627  1.9738

平均能量损失

\documentclass[tikz]{standalone}
\usepackage{pgf,pgfplots,pgfplotstable}
\usepackage{filecontents}           
\usepackage{amsmath,amssymb,bm}

\begin{document} 

\pgfplotstableread{myfile.dat}{\test}
\begin{tikzpicture}[]
    \begin{axis}[
        minor tick num=1,
        clip bounding box=upper bound,
        xlabel={x},
        ylabel={y},
        legend entries={PlotA, PlotB},
        legend style={at={(0.97,0.57)},anchor=south east, font =0.5\large}, 
        xmin=0, xmax=1, 
        ymin=0, ymax=4, 
        ]
        \addplot[black, line width=0.8pt] table [x={r}, y={M}] {\test};
        \addplot[red, line width=0.8pt] table [x={r}, y={m}] {\test};
    \end{axis}
\end{tikzpicture}

\end{document}

答案1

name path = A如果你已经包含了该fillbetween库,那么它应该可以正常工作。

\usepgfplotslibrary{fillbetween}.

在此处输入图片描述

\documentclass[tikz]{standalone}
\usepackage{pgf,pgfplots,pgfplotstable}
\usepgfplotslibrary{fillbetween}
\usepackage{filecontents}           
\usepackage{amsmath,amssymb,bm}

\begin{filecontents}{myfile.dat}
r           M       m   
0           2       2
0.010102    2.0202  2.0004
0.020204    2.0404  2.0005
0.030306    2.0606  2.0001
0.040408    2.0808  1.9993
0.05051     2.101   1.9981
0.060612    2.1212  1.9965
0.070714    2.1414  1.9945
0.080816    2.1616  1.9921
0.090918    2.1818  1.9893
0.10102     2.202   1.986
0.11112     2.2222  1.9824
0.12122     2.2424  1.9783
0.13133     2.2627  1.9738
\end{filecontents}
\begin{document} 

\pgfplotstableread{myfile.dat}{\test}
\begin{tikzpicture}[]
    \begin{axis}[
        minor tick num=1,
        clip bounding box=upper bound,
        xlabel={x},
        ylabel={y},
        legend entries={PlotA, PlotB},
        legend style={at={(0.97,0.57)},anchor=south east, font =0.5\large}, 
        xmin=0, xmax=1, 
        ymin=0, ymax=4, 
        ]
        \addplot[black, line width=0.8pt,name path=A] table [x={r}, y={M}] {\test};
        \addplot[red, line width=0.8pt, name path=B] table [x={r}, y={m}] {\test};
        \addplot[blue!50] fill between[of=A and B];
    \end{axis}
\end{tikzpicture}

\end{document}

相关内容