2

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[samples=100, domain=0:20, name path=A] {gammapdf(x,2,2)};
  \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} 

enter image description here

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.
4
  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,
        % add x axis label
        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,
    ]
        \addplot [samples=100,domain=0:20,name path=A] {gammapdf(x,2,2)};
        % 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)
        ;
        \addplot [gray,pattern=north west lines]
            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}];

        % add the textbox
        \node [anchor=north east,align=center] at (axis description cs:1,1)
            {shaded area = 0.7 \\ = critical ratio};
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

  • How can I get rid of the dotted horizontal line in the the shaded area? – per Apr 9 '16 at 22:48
  • @per, there is no "dotted horizontal line". This is just a rendering issue in the PDF viewer. – Stefan Pinnow Apr 10 '16 at 5:19

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.