pgfplots:填充曲线左侧的区域

pgfplots:填充曲线左侧的区域

我尝试填充曲线左侧,而不是下方。当我尝试填充中间时,我得到: 平均能量损失

相反,阴影应该位于曲线左侧,范围为 [1.5,3]。

我的代码:

\documentclass[preview]{standalone}
\linespread{1.25}
\usepackage{times}

\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\usetikzlibrary{positioning, arrows.meta}
\usepgfplotslibrary{fillbetween}
\usepackage{amsmath}

\begin{document}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
      scale = 1.2,
      xmin = 0, xmax = 3,
      ymin = 0, ymax = 3,
      axis lines = left,
      xtick = {0}, ytick = \empty,
      axis on top,
      clip = false,
      ]

      % curve
      \addplot [domain = 0:2, restrict y to domain = 0:3, samples=10000, thick, name path = curve]{exp(1.5-x)};

      % horizontal line
      \pgfmathsetmacro\myy{1.5}
      \pgfmathsetmacro\myx{1.09453489}
      \addplot [domain = 0:\myx, dashed, name path = line]{\myy};

      % vertical line
      \addplot[dashed] coordinates{(\myx,0) (\myx,\myy)};
      
      % shading
      \addplot [blue, opacity = 0.1] fill between [of = curve and line];
      
    \end{axis}
  \end{tikzpicture}
\end{center}

\end{document}

有人可以帮忙吗?

答案1

据我所知,fillbetweens 基本上是垂直的。你可以像这样把它们弄乱:

\documentclass[border=3mm]{standalone}
\usepackage{times}
\usepackage{pgfplots}
\pgfplotsset{compat = newest}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[
    % use a function here, to avoid magic number
    declare function = {
        myf(\x)=exp(1.5-x);
        invf(\y)=1.5-ln(\y);
        midx=invf(1.5);
        topx=invf(3.0);
    }
    ]
    \begin{axis}[
        scale = 1.2,
        xmin = 0, xmax = 3,
        ymin = 0, ymax = 3,
        axis lines = left,
        xtick = {0}, ytick = \empty,
        axis on top,
        clip = false,
        ]

        % curve: let's divide it in two
        \addplot [domain = topx:midx,  samples=100, thick, name path = curve]{myf(x)};
        \addplot [domain = midx:2, samples=100, thick]{myf(x)};

        % coordinate dashed line
        \draw [dashed] (0, 1.5) -- (midx,1.5) -- (midx,0);

        % shading
        \fill [blue, opacity = 0.1] (0,3) rectangle (topx,1.5);
        \path [name path=line] (topx,1.5) -- (midx,1.5);
        \addplot [blue, opacity = 0.1] fill between [of = line and curve];

    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

两个填充之间可能会有一条细细的白线 --- 这可能是由于查看器的精度有限;我不确定如何解决这个问题......

答案2

\documentclass[tikz, border=1cm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0, xmax=3,
ymin=0, ymax=3,
axis lines=left,
xtick={0}, ytick=\empty,
axis on top,
%y axis line style={name path=yaxis}, %does not work with `axis on top`
]
\path[name path=yaxis] (0,0) -- (0,3);
\addplot[domain=0:2, samples=50, smooth, thick,  name path=curve]{exp(1.5-x)};
\addplot[blue!20] fill between [of=curve and yaxis,  soft clip={domain y=1.5:3}];
\draw[dashed] (0, 1.5) -- ({1.5-ln(1.5)},1.5) -- ({1.5-ln(1.5)},0);
\end{axis}
\end{tikzpicture}
\end{document}

带曲线和填充区域的图形

相关内容