# Shading parts of a plot using pgfplots

I have the following plot that I need for my presentation. I have a minimal code for the same, however the shading is an issue. I am yet to be fully in grips over using PGFPlots.

\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\usetikzlibrary{decorations.markings}
\begin{figure}[h]
\centering
\begin{tikzpicture}[
declare function={gamma(\z)=
(2.506628274631*sqrt(1/\z) + 0.20888568*(1/\z)^(1.5) + 0.00870357*(1/\z)^(2.5) - (174.2106599*(1/\z)^(3.5))/25920 -
(715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z);},
declare function={gammapdf(\x,\k,\theta) = \x^(\k-1)*exp(-\x/\theta) / (\theta^\k*gamma(\k));}
]
\begin{axis}[
xmin=0, xmax=20,
ymin=0, ymax=0.3,
axis lines=middle,
]
\addplot[thick, samples=50, smooth,domain=0:6,magenta, name path=B ] coordinates {(7.4,0)(7.4,0.15)};
\path[name path=xaxis] (\pgfkeysvalueof{/pgfplots/xmin}, 0) -- (\pgfkeysvalueof{/pgfplots/xmax},0);
\addplot[gray, pattern=north west lines] fill between[of=A and xaxis, soft clip={domain=0:7.4}];
\end{axis}
\end{tikzpicture}
\end{figure}


I have two main queries:

1. I do not know how to mark the intersection as $Q^*$.
2. My shading does not work out.
3. Also I do not know how to write the text outside.

1. There are several commands how you can achieve this. I have chosen to set an xtick at that position and then named it using xticklabels.
2. You had everything right, but needed to add compat level to at least 1.11.
3. That is very simple: Just add a node somewhere in the plot.

For more details have a look at the comments in the code.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{
patterns,
pgfplots.fillbetween,
}
\pgfplotsset{
% by adding at least the compat' level 1.11 the fill between'
% library works fine; also then you can omit axis cs:' for the
% node coordinates
compat=1.11,
}
\begin{document}
\begin{tikzpicture}[
declare function={
gamma(\z)=
(2.506628274631*sqrt(1/\z) + 0.20888568*(1/\z)^(1.5)
+ 0.00870357*(1/\z)^(2.5) - (174.2106599*(1/\z)^(3.5))/25920
- (715.6423511*(1/\z)^(4.5))/1244160)*exp((-ln(1/\z)-1)*\z);
gammapdf(\x,\k,\theta)=
\x^(\k-1)*exp(-\x/\theta) / (\theta^\k*gamma(\k));
},
]
% define value for Q*
\def\Qstar{7.4}
\begin{axis}[
xmin=0, xmax=20,
ymin=0, ymax=0.2,
axis lines=middle,
xlabel=$Q$,
% change the position of xlabel'
xlabel style={
anchor=west,
},
% add xtick' only on the "Q*" position and name that tick appropriately
xtick={\Qstar},
xticklabels={$Q^*$},
% remove yticks'
ytick=\empty,
]
% draw a vertical path
\path [name path=B]
(\Qstar,\pgfkeysvalueof{/pgfplots/ymin}) --
(\Qstar,\pgfkeysvalueof{/pgfplots/ymax});
\path [name path=xaxis]
(\pgfkeysvalueof{/pgfplots/xmin},0) --
(\pgfkeysvalueof{/pgfplots/xmax},0)
;
fill between [of=A and xaxis, soft clip={domain=0:\Qstar}];
% draw the vertical line up to the intersection with the function
\draw [thick,magenta,intersection segments={of=A and B,sequence=R1}];