# add vertical line of equation x=2 and shade a region in graph by pgfplots

I would like to plot these four equations :

$$y=\dfrac{3}{2x+1}, y=3x-2, x=2 y=0$$


and i don't know how to draw vertical line of equation x=2 and shade a region like the picture below

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pst-plot}
\usepackage{pstricks}

\begin{document}

\begin{table}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
extra y ticks       = 2,
extra y tick labels = z,
extra y tick style  = { grid = major },
axis lines=middle,
grid=major,
xmin=-1,
xmax=3,
ymin=-1,
ymax=3,
xlabel=$x$,
ylabel=$y$,
xtick={-1,-0.5,0,...,3},
ytick={-1,-0.5,0,...,3},
legend style={at={(axis cs:-.5,0,1)}, anchor=north west,width=90cm, draw=none,opacity=.9, inner sep=2pt,fill=gray!10}]
%\addlegendentry{$f(x)=2^{x}$}
%\addlegendentry{$f(x)={\left(\dfrac{1}{2}\right)}^x$}
smooth,
enlargelimits=false,
axis line style={latex-latex},
\addplot[thick, samples=100, domain=0:6,blue] {3/(2*x+1)}; %node[right] {$f(x)=2^{x}$};
\addplot[thick, samples=50, smooth,domain=0:6,olive] {3*x-2}; % node[right]{$f(x)={\dfrac{1}{2}}^x$};
\end{axis}
\end{tikzpicture}
\end{table}

\end{document}


which produces :

• This is how you get the vertical line: tex.stackexchange.com/questions/56896/… – MaxNoe Apr 26 '15 at 10:11
• If you know your y-limits you can also simply use \addplot coordinates {(2, -1) (2, 3)}; – MaxNoe Apr 26 '15 at 10:19
• line without points and how can i shade that region – Educ Apr 26 '15 at 10:24
• add +[mark=none] to the command: \addplot +[mark=none] coordinates {(2, -1) (2, 3)}; – MaxNoe Apr 26 '15 at 10:33

I reduced your code a bit, but this is what you need:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\begin{document}

\begin{figure}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
xmin=-1, xmax=3,
ymin=-1, ymax=3,
axis lines=middle,
]
\path[name path=xaxis] (\pgfkeysvalueof{/pgfplots/xmin}, 0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot[gray, pattern=north west lines] fill between[of=B and xaxis, soft clip={domain=0.66666:1}];
\addplot[gray, pattern=north west lines] fill between[of=A and xaxis, soft clip={domain=1:2}];
\addplot +[mark=none] coordinates {(2, -1) (2, 3)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}


The result is:

Here is a over kill solution for you.

\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}

\pgfplotsset{compat=1.12}

\begin{document}

\begin{table}[h]
\centering
\begin{tikzpicture}
\begin{axis}[
extra y ticks       = 2,
extra y tick labels = z,
extra y tick style  = { grid = major },
axis lines=middle,
grid=major,
xmin=-1,
xmax=3,
ymin=-1,
ymax=3,
xlabel=$x$,
ylabel=$y$,
xtick={-1,-0.5,0,...,3},
ytick={-1,-0.5,0,...,3},
legend style={at={(axis cs:-.5,0,1)}, anchor=north west,width=90cm, draw=none,opacity=.9, inner sep=2pt,fill=gray!10}]
%\addlegendentry{$f(x)=2^{x}$}
%\addlegendentry{$f(x)={\left(\dfrac{1}{2}\right)}^x$}
smooth,
enlargelimits=false,
axis line style={latex-latex},
\addplot[thick, samples=100, domain=0:6,blue,name path=one] {3/(2*x+1)}; %node[right] {$f(x)=2^{x}$};
\addplot[thick, samples=50, smooth,domain=0:6,olive,name path=two] {3*x-2}; % node[right]{$f(x)={\dfrac{1}{2}}^x$};
\addplot[thick, samples=50, smooth,domain=0:6,magenta, name path=three] coordinates {(2,-1)(2,3)};
\path[name path=axis] (0,0) -- (3,0);
\path[name path=lower,
%draw=red,ultra thick,
intersection segments={of=one and two,sequence=R1 -- L2}
];
fill between[
of=axis and lower,
soft clip={
domain=0:2
},
split,
every segment no 0/.style={fill=none},
every segment no 1/.style={pattern=grid,pattern color=orange},
];
\end{axis}
\end{tikzpicture}
\end{table}

\end{document}


• Please verify ur code it's not working for me by the way i'm using ttexniccenter under windows – Educ Apr 26 '15 at 18:04
• @Educ Not working is not enough information as this code compiles for me. Do you have an up to date tex distribution? What is the error you get? – user11232 Apr 26 '15 at 23:26
• before i make update and tell you what i my version, there is 56 errors for example latex Error: \begin{tikzpicture} on input line 12ended by \end{document} – Educ Apr 27 '15 at 2:50
• @Educ: You did something extra then. I don't get any error. – user11232 Apr 27 '15 at 2:52
• i got another error " you can't use '\end' in internal vertical mode" – Educ Apr 27 '15 at 2:57