3

I am trying to fill to the left of a curve, rather than below it. When I tried fill between, I got: MWE

Instead, the shading should be to the left of the curve, on the range [1.5,3].

My code:

\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}

Can anyone help?

2 Answers 2

2

For what I know, fillbetweens are basically vertical. You can trick the things out like this:

\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}

enter image description here

You can have a thin white line between the two fills --- due probably to finite precision on the viewer; I am not sure how to solve that...

2
  • +1 fillbetween constructs a new path used in \fill, so it also has no preferred direction(whatever that would mean). Commented Feb 4 at 14:37
  • Only now do I see, what is actually going on in your code - you have two fills to cover the area. -that would not be needed, if you fill between the curve and a vertical line. Instead of using opacity (which I assume is not needed/intended) you fill with ligh blue, then there could be an overlap. Commented Feb 4 at 14:48
1
\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}

Graph with curve and filled area

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .