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.

    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 - 
    declare function={gammapdf(\x,\k,\theta) = \x^(\k-1)*exp(-\x/\theta) / (\theta^\k*gamma(\k));}
    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}];

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.

1 Answer 1

  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.

        % 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
    declare function={
            (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);
            \x^(\k-1)*exp(-\x/\theta) / (\theta^\k*gamma(\k));
        % define value for Q*
        xmin=0, xmax=20,
        ymin=0, ymax=0.2,
        axis lines=middle,
        % add x axis label
        % change the position of `xlabel'
        xlabel style={
        % add `xtick' only on the "Q*" position and name that tick appropriately
        % remove `yticks'
        \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}) --
        \path [name path=xaxis]
            (\pgfkeysvalueof{/pgfplots/xmin},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};

image showing the result of above code

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .