TikZ diagrams for economists: A normal pdf with shaded area.

I have been dabbling with the TikZ package to create some diagrams relevant to a first year microeconomics course. The following diagram of the probability density function (pdf) of a normal distribution may be useful to others wishing to integrate similar diagrams into their LaTeX documents or Beamer presentations. To use, insert the following code anywhere you like within a .tex document (you must include \usepackage{tikz} in your header):

The Cumulative Density of y

INSERT INTO .TEX DOCUMENT

\begin{tikzpicture}
% define normal distribution function 'normaltwo'
    \def\normaltwo{\x,{4*1/exp(((\x-3)^2)/2)}}

% input y parameter
    \def\y{4.4}

% this line calculates f(y)
    \def\fy{4*1/exp(((\y-3)^2)/2)}

% Shade orange area underneath curve.
    \fill [fill=orange!60] (2.6,0) -- plot[domain=0:4.4] (\normaltwo) -- ({\y},0) -- cycle;

% Draw and label normal distribution function
    \draw[color=blue,domain=0:6] plot (\normaltwo) node[right] {};

% Add dashed line dropping down from normal.
    \draw[dashed] ({\y},{\fy}) -- ({\y},0) node[below] {$y$};

% Optional: Add axis labels
    \draw (-.2,2.5) node[left] {$f_Y(u)$};
    \draw (3,-.5) node[below] {$u$};

% Optional: Add axes
    \draw[->] (0,0) -- (6.2,0) node[right] {};
    \draw[->] (0,0) -- (0,5) node[above] {};

\end{tikzpicture}

The Probability of u Falling Between x and y

INSERT INTO .TEX DOCUMENT

\begin{tikzpicture}
% define normal distribution function 'normaltwo'
    \def\normaltwo{\x,{4*1/exp(((\x-3)^2)/2)}}

% input x and y parameters
    \def\y{4.4}
    \def\x{3.4}

% this line calculates f(y)
    \def\fy{4*1/exp(((\y-3)^2)/2)}
    \def\fx{4*1/exp(((\x-3)^2)/2)}

% Shade orange area underneath curve.
    \fill [fill=orange!60] ({\x},0) -- plot[domain={\x}:{\y}] (\normaltwo) -- ({\y},0) -- cycle;

% Draw and label normal distribution function
    \draw[color=blue,domain=0:6] plot (\normaltwo) node[right] {};

% Add dashed line dropping down from normal.
    \draw[dashed] ({\y},{\fy}) -- ({\y},0) node[below] {$y$};
    \draw[dashed] ({\x},{\fx}) -- ({\x},0) node[below] {$x$};

% Optional: Add axis labels
    \draw (-.2,2.5) node[left] {$f_Y(u)$};
    \draw (3,-.5) node[below] {$u$};

% Optional: Add axes
    \draw[->] (0,0) -- (6.2,0) node[right] {};
    \draw[->] (0,0) -- (0,5) node[above] {};

\end{tikzpicture}

The TikZ code snippet above is meant to be dropped into a .tex document and work without any further “tinkering”. Please let me know if this is not the case!

Advertisements
Tags: , ,

7 Comments to “TikZ diagrams for economists: A normal pdf with shaded area.”

  1. Thanks for the code. This is very useful – I used it as the foundation for material I’m typesetting for my students and you’ve deepened my LateX expertise.

  2. Hi,
    This is indeed very useful! Well done.

    How do I change this part so that the shaded area is at both ends?

    \fill [fill=orange!60] ({\x},0) — plot[domain={\x}:{\y}] (\normaltwo) — ({\y},0) — cycle;

    Thank you very much.

  3. Hi, very nice post. I used the code for a presentation and found out that it is straightfoward to make the curve a bit smoother using the following:

    \draw[color=blue,domain=0:6] plot[samples=1000] (\normaltwo) node[right] {};

    Thank you very much.

  4. What’s up, everything is going well here and ofcourse every one is sharing data, that’s actually excellent, keep
    up writing.

  5. Thanks a lot! 🙂 this helped me more than the questions on stackexchange. at least it has comments 🙂

  6. Hi, Thanks for sharing this. I have used this example in my thesis, could you point me out how should I give you credit? Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: