在不同轴环境中的两条曲线之间填充

在不同轴环境中的两条曲线之间填充

我正在尝试填充两条曲线之间的区域。用 很容易\fillbetween,对吧?只是这两条曲线是在两个不同的轴上绘制的,因此name path它们之间会丢失 。

为了得到一个想法,这里是来自 ' 文档的 MWE pgfplots。有没有简单的方法来填充该区域?

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
  \pgfplotsset{set layers}
  \begin{axis}[
   scale only axis,
   xmin=-5,xmax=5,
   axis y line*=left,
   xlabel=$x$,
   ylabel=First ordinate]
   \addplot {x^2};
  \end{axis}
  \begin{axis}[
   scale only axis,
   xmin=-5,xmax=5,
   axis y line*=right,
   axis x line=none,
   ylabel=Second ordinate]
   \addplot[red] {3*x};
   \node[draw,align=center] at (axis cs:1,-5)  {Fill\\here};
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

使用name path global而不是name path

在此处输入图片描述

\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.12}
\begin{document}

\begin{tikzpicture}
  \pgfplotsset{set layers}
  \begin{axis}[
   scale only axis,
   xmin=-5,xmax=5,
   axis y line*=left,
   xlabel=$x$,
   ylabel=First ordinate]
   \addplot [name path global=a] {x^2};
  \end{axis}
  \begin{axis}[
   scale only axis,
   xmin=-5,xmax=5,
   axis y line*=right,
   axis x line=none,
   ylabel=Second ordinate]
   \addplot[red,name path=b] {3*x};
   \node[draw,align=center] at (axis cs:1,-5)  {Fill\\here};
   \addplot [opacity=0.3] fill between[of=a and b];
  \end{axis}
\end{tikzpicture}

\end{document}

相关内容