1

In tikzpicture, within an axis environment I am uploading a figure with

\addplot graphics[xmin=0,ymin=-.15,xmax=10,ymax=1.1] {figure.png};

On top of this figure I can draw two lines

\addplot [name path=line1,color=red] table[row sep=crcr]{...data1...};
\addplot [name path=line3,color=red] table[row sep=crcr]{...data2...};

Now I would normally add a uniform filling between line1 and line2 with

\addplot[blue] fill between[of=line1 and line2];

However this does not work as it is overlapped by the graphics added previously. Is there a way to have the filling overlapping the graphics? How else can I do it?

A MWE would be

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{figure}
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[xmin=0,ymin=0,xmax=4,ymax=16]
\addplot [name path=line1,color=red, line width=1.5pt] 
  table[row sep=crcr]{%    
0   0\\
1   1\\
2   4\\
3   9\\
4   16\\
};
\addplot graphics[xmin=0,ymin=0,xmax=4,ymax=16] {figure.png};
\addplot [name path=line2,color=blue, line width=1.5pt]
  table[row sep=crcr]{%
0                   0\\
1                   1\\
2                   2\\
3                   3\\
4                   4\\
};
\addplot[olive] fill between[of=line1 and line2];
\end{axis}
\end{tikzpicture}%
\end{figure}
\end{document}
  • Welcome to TeX.SX! The first thing you should do is adding a minimal working example (MWE) that illustrates your problem. Reproducing the problem and finding out what the issue is will be much easier when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – CarLaTeX Feb 22 '17 at 16:40
  • 2
    Perhaps you could have a look at the on layer key (section 4.27.3 * Changing the Layer of Graphical Elements* in the manual). – Torbjørn T. Feb 22 '17 at 16:42
1

It is sufficient to add on layer=axis background in the options of addplot. The following code would work

\documentclass{article}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{figure}
\usepgfplotslibrary{fillbetween}
\begin{tikzpicture}
\begin{axis}[xmin=0,ymin=0,xmax=4,ymax=16]
\addplot [name path=line1,color=red, line width=1.5pt] 
  table[row sep=crcr]{%    
0   0\\
1   1\\
2   4\\
3   9\\
4   16\\
};
\addplot[on layer=axis background] graphics[xmin=0,ymin=0,xmax=4,ymax=16] {figure.png};
\addplot [name path=line2,color=blue, line width=1.5pt]    
  table[row sep=crcr]{%
0                   0\\
1                   1\\
2                   2\\
3                   3\\
4                   4\\
};
\addplot[olive] fill between[of=line1 and line2];
\end{axis}
\end{tikzpicture}%
\end{figure}
\end{document}
| improve this answer | |
  • It would help to use example-image and post a picture. – John Kormylo Feb 22 '17 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.