7

Consider the following MWE:

\documentclass{article}
\usepackage{tikz, pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}
        \addplot [domain=0.7:1.6,samples=20, fill=red!50!white] 
            {(1.5*x - 4.5)^2/(10*x) + 1} \closedcycle ;
        \addplot [domain=0.7:1.6,samples=20, blue, very thick] 
            {(1.5*x - 4.5)^2/(10*x) + 1} 
            node[at start, above, black] {$(x1,y1)$} 
            node[at end, above, black]   {$(x2,y2)$};
    \end{axis}
\end{tikzpicture}
\end{document}

which produce this

enter image description here

What I want is to

  1. Fill the area between the blue curve and the x-axis (red fill)
  2. Fill the area between the blue curve and the y-axis, that is, indicating the region interval y2 to y1 on the y-axis.

I was hoping there is some way to specify \closedcycle with respect to the y-axis or similar.

A solution could of course be to calculate the points (x1,y1) and (x2,y2), but it feels very un-TikZy.

(This means that there will be a white rectangle defined by the origin, (x1,0), (x1,y2) and (0,y2))

7
  • You can draw a second function. Commented May 11, 2013 at 13:51
  • Could you please elaborate?
    – Holene
    Commented May 11, 2013 at 13:52
  • Would this be OK : {(1.5*x - 4.5)^2/(10*x) + 1} |- (axis description cs:0,0) |- (current plot begin) ; instead of \closedcycle ?
    – percusse
    Commented May 11, 2013 at 13:54
  • I mean \addplot [domain=0:0.7,samples=2, fill=red!20] {(1.5*0.7 - 4.5)^2/(10*0.7) + 1} \closedcycle ; Commented May 11, 2013 at 13:55
  • Neither works. I'll update the question in a bit.
    – Holene
    Commented May 11, 2013 at 14:32

1 Answer 1

9

You can define a new command \closedcycley that does the same as the normal \closedcycle, but for the y axis. If you then plot the function twice, once with \closedcycle and once with \closedcycley, both times slightly transparently, you'll get the following:

\documentclass[border=5mm]{standalone}
\usepackage{tikz, pgfplots}

\makeatletter
\def\closedcycley{%
    -| (perpendicular cs: 
        horizontal line through={(current plot begin)}, 
        vertical line through={(\pgfplots@ZERO@x,\pgfplots@ZERO@y)})
    -- cycle
}%
\makeatother

\begin{document}
\begin{tikzpicture}
    \begin{axis}[axis on top]
        \addplot [opacity=0.5,domain=0.7:1.6,samples=20, fill=red!50!white, draw=none] 
            {(1.5*x - 4.5)^2/(10*x) + 1} \closedcycle ;
        \addplot [opacity=0.5, domain=0.7:1.6,samples=20, fill=red!50!white, draw=none] 
            {(1.5*x - 4.5)^2/(10*x) + 1} \closedcycley ;
        \addplot [domain=0.7:1.6,samples=20, blue, very thick] 
            {(1.5*x - 4.5)^2/(10*x) + 1} 
            node[at start, above, black] {$(x1,y1)$} 
            node[at end, above, black]   {$(x2,y2)$};
    \end{axis}
\end{tikzpicture}
\end{document}

You must log in to answer this question.

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