1

Using the shadows TikZ library, we can add a drop shadow to a rectangle. Is there a way to add the same effect to the boxed axis of a pgfplots picture? Here is a minimal example:

\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{shadows}
\begin{document}
\begin{tikzpicture}
\draw[fill=white, drop shadow] (0,0) rectangle (5, 5);
\end{tikzpicture}

\vspace*{1.0\baselineskip}
\begin{tikzpicture}
\begin{axis}
\addplot {x^2};
\end{axis}
\end{tikzpicture}
\end{document}

which produces this: picture example

1

Two options:

  1. Working inside the axis environment, you can use the native layer system of pgfplots together with the rel axis coordinate system to add the shadow (requires clip=false):

    \documentclass{article}
    \usepackage{pgfplots}
    \usetikzlibrary{shadows}
    
    \begin{document}
    
    \begin{tikzpicture}
    \begin{axis}[set layers,clip=false]
    \addplot {x^2};
    \draw[fill=white, drop shadow,on layer=axis background] 
      (rel axis cs:0,0) rectangle (rel axis cs:1,1);
    \end{axis}
    \end{tikzpicture}
    \end{document}
    

    enter image description here

  2. Working outside the axis environment, you can name the axis and then use the name to place the shadow on the background using the tikz backgrounds library:

    \documentclass{article}
    \usepackage{pgfplots}
    \usetikzlibrary{shadows,backgrounds}
    
    \begin{document}
    
    \begin{tikzpicture}
    \begin{axis}[name=myaxis]
    \addplot {x^2};
    \end{axis}
    \begin{pgfonlayer}{background}
    \draw[fill=white, drop shadow] (myaxis.north west) rectangle (myaxis.south east);
    \end{pgfonlayer}
    \end{tikzpicture}
    
    \end{document}
    

    enter image description here

As a side note, pgfplots internally loads TikZ, so no need to load the latter when you load the former. The minimal class is meant to serve to test an installation with something like the classic "Hello world!". I'd suggest you not to use if for anything more complex than that.

|improve this answer|||||

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.