0

I am plotting two lines based on data that I am importing into PGF. I would like then to shade the region between the two lines but I am not sure how. I have tried name path = A etc as suggested in this post, but that didn't work.

Presume the data is something simple such as:

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

MWE

\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}
Sid
  • 1,806

1 Answers1

1

name path = A should work fine if you have included the fillbetween library.

\usepgfplotslibrary{fillbetween}.

enter image description here

\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}
nidhin
  • 7,932