0

I would really like to have a simple explanation to how the path and fill functions work in TikZ.

For instance, I don't know why I am unable to fill the whole area under the curve despite putting a line like this :

plot    ({\x}, {2*exp(-\x/5)+ 1/2}) |- (0,0) ;

enter image description here

\documentclass{article}

\usepackage{amsmath}
\usepackage{accents} 
\usepackage{pgf, tikz, adjustbox}
\usetikzlibrary{patterns, matrix, positioning}
\usetikzlibrary{arrows.meta,
                patterns.meta
                }

\begin{document}

\begin{tikzpicture}[
dot/.style = {circle, fill, inner sep=2.0pt, node contents={}},
every label/.append style = {label distance =2pt, inner sep=1pt, fill=white},
samples = 101,
domain = 0:10
]
\draw[-stealth] (-2.5, 0) -- (13,  0) node[right] {$\infty$}; % x-axis
\draw[-stealth] (0, -0.5) -- ( 0,4.3) node[above] {$\underline{e}_z$}; % y-axis
\foreach \i in {-2,-1,0, ...,12} 
\draw   (\i, 1mm) -- ++ (0,-2mm);
\draw[very thick, blue]    
plot    ({\x}, {2*exp(-\x/5) + 1/2}) -- (11,0.75);
%    \fill [gray, domain=0:10, variable=\x]
 %     (0, 0)
  %    -- plot ({\x}, {2*exp(-\x/5)+ 1/2})
    %  -- (11, 0)
     % -- cycle;
\path[pattern={Lines[angle=45,distance={3pt},
line width=0.2pt]},
pattern color=blue]
plot    ({\x}, {2*exp(-\x/5)+ 1/2}) |- (0,0) ;
\path  (11,0.75) node[red,dot,label = right:$h_\infty$];
\path   (0,0) node[black,dot,scale =0.75,label=below left:$L$];
\end{tikzpicture}


\end{document}

I don't understand how the filling process works. For example if I try the \fill function:

   \fill [gray, domain=0:10, variable=\x]
      (0, 0)
      -- plot ({\x}, {2*exp(-\x/5)+ 1/2})
      -- (11, 0)
      -- cycle;

With (11,0) the last point of the curve then I get this :

enter image description here

It's more a general question about how to use the filling process very simply and efficiently.

I have read the section of the TikZ manual related to this, but I still don't understand it...

2

1 Answer 1

3
  • You draw your function (thick blue line) in two parts: as plot of function on domain=:10 (defined in picture preamble) and from its end as straight line from its end to coordinate (11,0.75)
  • Pattern you draw between function and path from end of function to x-axis and coordinate origin: ... ({\x}, {2*exp(-\x/5)+ 1/2}) |- (0,0)
  • but grey filled area you first draw straight line from function to coordinate (11,0) and than to coordinate (origin):... ({\x}, {2*exp(-\x/5)+ 1/2}) -- (11,0) -- (0,0)
  • If you like that gray fill cover the same area as pattern, than you should use the same area as at pater, but if you like that gray fill right border is at right red point, than grey filled area should be defined by
\fill [gray!30] plot    ({\x}, {2*exp(-\x/5) + 1/2}) -- (11,0.75) |- (0,0);

or

\fill [gray!30, domain=0:11] plot    ({\x}, {2*exp(-\x/5) + 1/2}) |- (0,0);

(observe use |- at path definition).

enter image description here

  • However, I guess, that you like that pattern right border is at the end of blue line. In this case you only need to change function domain:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{patterns, matrix, positioning}
\usetikzlibrary{arrows.meta,
                patterns.meta
                }

\begin{document}

\begin{tikzpicture}[x=8mm,  % <---
dot/.style = {circle, fill=#1, inner sep=2.0pt, node contents={}},  % <---
every label/.append style = {label distance =2pt, inner sep=1pt},   % <---
samples = 101,
domain = 0:11      % <---
]
\draw[-stealth] (-1.5, 0) -- (13,  0) node[right] {$\infty$}; % x-axis
\draw[-stealth] (0, -0.5) -- ( 0,4.3) node[above] {$\underline{e}_z$}; % y-axis
\foreach \i in {-1,0, ...,12}
\draw   (\i, 1mm) -- ++ (0,-2mm);
%
\draw[very thick, blue] plot    ({\x}, {2*exp(-\x/5) + 1/2}) 
                node[dot=red, label = right:$h_\infty$]; % <---
%
\path[pattern={Lines[angle=45,distance={3pt},
      line width=0.2pt]},
      pattern color=blue] plot ({\x}, {2*exp(-\x/5)+ 1/2}) |- (0,0) ;
%
\path   (0,0)   node[dot=black,scale =0.75,label=below left:$L$]; % <---
\end{tikzpicture}

\end{document}

which produce:

enter image description here

Changes in code in comparison to your MWE are marked by `% <---%. In code are done the following changes:

  • redefined is style for dot, now is added option for its color
  • changed is domain of your function (from 0:10) to (0:11)
  • changed are size of x units from default 1cm to 8mm (that diagram has less width)
  • red dot is moved to the end of curve and is not drawn separately on manually calculated coordinate.

Edit: If above solution is most efficient, I do not know. It may be 😊. Depends on how you define efficiency. Anyway, function is drawn as I have learned in school: above its domain. On the same way is at defined the borders of pattern area.

2
  • Thanks a lot for taking time to explain ! Also : I guess my confusion reminds on this line plot ({\x}, {2*exp(-\x/5) + 1/2}) -- (11,0.75); : so the function is defined on the domain 0:10 but the draw line is plotted to the point (11,0.75). But I don't understand how the code allows me to go beyond the domain (limited to 10) why the line is "authorized" to be drawn until the point (11,0.75) ? Thank you again
    – Wiss
    Jun 21 at 9:20
  • Function is drawn by \plot only over domain. However, out of domain you still can draw lines or what ever, even starting at end of function. Let me repeat, It is total unclear, why you draw blue line after end of curve defined by your function. Of course, as you draw borders of shading area, it is combination of four segments: (0, 0) -- (0,{2*exp(-0/5)+1/2}), plot ({\x}, {2*exp(-\x/5)+ 1/2}), plot (10,{2*exp(-10/5)+1/2}) -- (11, 0) and (11, 0) -- (0.0). To see this segments, consider @user202729 in comment below your question.
    – Zarko
    Jun 21 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.