如何正确获取 pgfplots 图例的阴影?

如何正确获取 pgfplots 图例的阴影?

legend style当使用图例框和图例图像中的线条创建的绘图中使用阴影时,pgfplots最终图片中会“投下阴影”。由于图例图像中的标记不会“投下阴影”,我认为这不是预期的行为。有没有办法去除图例框中的线条阴影?

\documentclass{standalone}

\usepackage{pgfplots}
\usetikzlibrary{shadows}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  small,
  legend style={
    rounded corners=3pt, legend pos=north west,
    drop shadow={fill=black, opacity=0.5, shadow xshift=2pt, shadow
      yshift=-2pt}
    }
    ]
  \addplot[thick, blue]{x};
  \addplot[thick, red]{2*x};
  \legend{$x$,$2x$}
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

我认为,问题在于阴影会被图例图像自动继承。

为了应对这种情况,可以利用late options类似的方法append after command

以下是一个例子:

\documentclass[border=10pt,png]{standalone}

\usepackage{pgfplots}
\usetikzlibrary{shadows,shadows.blur}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  small,
  legend pos=north west,
  legend style={rounded corners=3pt,
    append after command={
    \pgfextra{
      \draw[rounded corners=3pt,
      drop shadow={fill=black, opacity=0.5, shadow xshift=2pt, shadow yshift=-2pt}]
        (\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
      }
      }
  },
  ]
  \addplot[thick, blue]{x};
  \addplot[thick, red]{2*x};
  \legend{$x$,$2x$}
\end{axis}
\end{tikzpicture}

\end{document}

结果:

在此处输入图片描述

代码改进

为了能够更自动化地将阴影应用于图例,这里有一堆键/样式;它们允许指定:

  • 类似或任何其他适用于路径的选项legend path optionsrounded corners
  • ,即legend shadow options只涉及阴影的特定选项
  • use legend shadowed只是一段代码的快捷方式:
      附加在命令之后={
        \pgfextra{
          \draw[圆角=3pt,
          阴影={填充=黑色,不透明度=0.5,阴影 xshift=2pt,阴影 yshift=-2pt}]
            (\tikzlastnode.西南)矩形(\tikzlastnode.东北);
          }
       }

以前的 MWE 现在变为:

\documentclass[border=10pt,png,tikz]{standalone}

\usepackage{pgfplots}
\usetikzlibrary{shadows}


\tikzset{
  legend shadow/.style 2 args={% #1 path options, #2 shadow options 
  append after command={
    \pgfextra{
      \draw[#1,drop shadow={#2}] 
       (\tikzlastnode.south west)rectangle(\tikzlastnode.north east);
      }
    }
  },
  use legend shadowed/.style={legend shadow={path options}{shadow options}},
  path options/.style={},
  shadow options/.style={},
  legend path options/.code={
    \tikzset{path options/.append style={#1}}
  },
  legend shadow options/.code={
    \tikzset{shadow options/.append style={#1}}
  },
}

\begin{document}

\begin{tikzpicture}[
  legend path options={rounded corners=3pt},
  legend shadow options={black,
    opacity=0.5,
    shadow xshift=2pt, 
    shadow yshift=-2pt
  }
]
\begin{axis}[
  small,
  legend pos=north west,
  legend style={rounded corners=3pt,
    use legend shadowed,
  },
  legend entries={$x$,$2x$} 
  ]
  \addplot[thick, blue,mark=*]{x};
  \addplot[thick, red]{2*x};
  \legend{$x$,$2x$}
\end{axis}
\end{tikzpicture}

\end{document}

免责声明

为简单起见,键legend path optionslegend shadow options已放在路径下/tikz/。这意味着它们不能可用于legend style

\begin{axis}[
  small,
  legend pos=north west,
  legend style={rounded corners=3pt,
    use legend shadowed,
    legend path options={rounded corners=3pt},
    legend shadow options={black,
      opacity=0.5,
      shadow xshift=2pt, 
      shadow yshift=-2pt
    }
  },
  legend entries={$x$,$2x$} 
  ]

相关内容