Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I defined an area bounded by four functions. I found related answers like: "filling area between 2 functions, with shading", but I am not able to adapt this. solution.

Here the example:

% !TEX program  = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis equal image,
width=10cm,
axis lines=middle,
axis line style={-latex},
clip =false,
xmin=0,xmax=2.45,
ymin=0,
no markers,
xlabel={$x$},ylabel={$y$},
every axis x label/.append style={anchor=north},
every axis y label/.append style={anchor=east},
]
\addplot[domain=0.4:1.2,thick] { 1/x } node[anchor=135] {$f(x)=\dfrac{1}{x}$};
\addplot[domain=.6:2,thick] { 2/x } node[right] {$f(x)=\dfrac{2}{x}$};
\addplot[domain=0.7:1.5,thick] { x } node[right] {$f(x)=x$};
\addplot[domain=0.4:.8,thick] { 4*x } node[right] {$f(x)=4x$};


%\addplot+[domain=0.5:1] { 1/x };
%\addplot+[domain=1:sqrt(2)] { x };
%\addplot+[domain=1/sqrt(2):sqrt(2)] { 2/x };
%\addplot+[domain=0.5:1/sqrt(2)] { 4*x };
\end{axis}
\end{tikzpicture}
\end{document}

With the result:

enter image description here

I want to fill the area with vertical lines or maybe simple gray.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

I found a way.

The environment needs the option area style to draw correct axis.

Now I draw every function again with the command closedcycle where the functions above are filled with gray and the functions below fill with white. (draw over).

% !TEX program  = pdflatex
% !TEX encoding = UTF-8 Unicode
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{amsmath}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis equal image,
width=10cm,
axis lines=middle,
axis line style={-latex},
clip =false,
xmin=0,xmax=2.45,
ymin=0,
no markers,
xlabel={$x$},ylabel={$y$},
every axis x label/.append style={anchor=north},
every axis y label/.append style={anchor=east},
area style,
]

\addplot+[domain=1/sqrt(2):sqrt(2),fill,gray] { 2/x }  \closedcycle;
\addplot+[domain=0.5:1/sqrt(2),fill,gray] { 4*x }  \closedcycle;
\addplot+[domain=0.5:1,fill,white] { 1/x } \closedcycle;
\addplot+[domain=1:sqrt(2),fill,white] { x }  \closedcycle;

\addplot[domain=0.4:1.2,thick] { 1/x } node[anchor=135] {$f(x)=\dfrac{1}{x}$};
\addplot[domain=.6:2,thick] { 2/x } node[right] {$f(x)=\dfrac{2}{x}$};
\addplot[domain=0.7:1.5,thick] { x } node[right] {$f(x)=x$};
\addplot[domain=0.4:.8,thick] { 4*x } node[right,] {$f(x)=4x$};

\node at (axis cs:1,1.5) {$D$};

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer

tkz-fct can't answer properly but it's relatively easy to make it with tikz. Before the "clip", I use the macros of tkz-fct to draw the graphs with gnuplot.

enter image description here

\documentclass{scrartcl}
\usepackage{tkz-fct}   
\begin{document}

\begin{tikzpicture}[xscale=2,yscale=2,line width=0.6pt,>=latex]
  \tkzInit[xmax=3,ymax=3] 
  \tkzGrid
  \tkzAxeXY 
  \tkzFct[domain=0.5:2]{2/x}   
  \tkzFct[domain=0.2:1.5]{1/x}  
  \tkzFct[domain=0.2:1]{4*x} 
  \tkzFct[domain=0.5:1.5]{x}  
  \clip (0.5,0) -- plot[domain=0.5:2] function{4*x} -- (2,0)--cycle;
  \clip (0.5,3) -- plot[domain=0.5:2] function{x}   -- (2,3)--cycle;
  \clip (0.5,0) -- plot[domain=0.5:2] function{2/x} -- (2,0)--cycle;
  \clip (0.5,3) -- plot[domain=0.5:2] function{1/x} -- (2,3)--cycle;     
  \fill[blue!20,fill opacity=.5] (0.5,0) rectangle (2,3);
\end{tikzpicture}  
\end{document} 

With tkz-fct the answer is shorter for the final steps:

\begin{tikzpicture}[xscale=2,yscale=2,line width=0.6pt,>=latex]
  \tkzInit[xmax=3,ymax=3] 
  \tkzGrid
  \tkzAxeXY 
  \tkzFct[domain=0.5:2]{2/x}   
  \tkzFct[domain=0.2:1.5]{1/x}  
  \tkzFct[domain=0.2:1]{4*x} 
  \tkzFct[domain=0.5:1.5]{x}  
  \clip (0.5,0) -- plot[domain=0.5:2] function{4*x} -- (2,0)--cycle;
  \clip (0.5,3) -- plot[domain=0.5:2] function{x}   -- (2,3)--cycle;  
  \tkzDrawAreafg[between= a and b,color = blue!20,domain = 0.5:1.5]    
\end{tikzpicture}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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