2

I am trying to draw the picture below.

enter image description here

The four graphs are each a density probability function; the distribution in case 1 and case 2 is unimodal, in case 3 it's normal, and in case 4 it's bimodal. I wan't the curves to look like a sketch, like in the picture below.

I don't know the best method to draw simple curves in the tikz environment. I have tried using the command \draw plot [smooth cycle] coordinates {(x0,y0), ... }; but it is very time demanding to do for each curve.

Instead i tried doing something with inspiration from the post How to create a gaussian curve with pgfplot? - only for the curve in case 3. I still have a problem with achieving the right width of the curve.

My code is below:

\begin{tikzpicture}

% bell curve
\newcommand\gauss[2]{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))} 

% origo
\node[circle,fill=black,inner sep=1pt] (origo) at (0,0) {};

% y axis
\draw[->] (origo) -- (0,6);

% horizontal lines
\draw[->] (-3,0) -- (3,0);
\draw[->] (-3,1.5) -- (3,1.5);
\draw[->] (-3,3) -- (3,3);
\draw[->] (-3,4.5) -- (3,4.5);

% node
\node[right,inner sep=4pt,outer sep=4pt] at (3,4.5) {\large{$x$}};

% case nodes
\node[left, inner sep=4pt,outer sep=4pt] at (-3,4.5) {\large{1}};
\node[left, inner sep=4pt,outer sep=4pt] at (-3,3) {\large{2}};
\node[left, inner sep=4pt,outer sep=4pt] at (-3,1.5) {\large{3}};
\node[left, inner sep=4pt,outer sep=4pt] at (-3,0) {\large{4}};

% curves
\begin{axis}[hide axis,every axis plot post/.append style={
  mark=none,domain=-3:3,samples=50,smooth},xshift=-0.95cm,yshift=1.5cm,height=3cm]
\addplot {\gauss{0}{1.7}};
\end{axis}

\end{tikzpicture}

And it produces: enter image description here

3

I think your problem is that you're not defining the width of the axis, so the default width is used. Looks like your lines are 6cm wide, so set width=6cm. There are a number of other settings that would be useful I think, here is a modified axis environment with some comments:

\begin{axis}[
  hide axis,
  enlargelimits=false, % removes whitespace between plot and axis
  clip=false, % avoids clipping of plot lines at the axis border
  width=6cm,height=1.3cm, % set size of axis
  scale only axis, % don't inlude any ticks, labels etc. in the size calculation
  every axis plot post/.append style={
    mark=none,
    domain=-3:3,
    samples=50,
    smooth},
  anchor=origin, % set the anchor of the axis to the origin
  at={(0,1.5cm)} % define the position of the axis in the coordinate system of the tikzpicture
  ]
\addplot {\gauss{0}{1.7}};
\end{axis}

Leaving the rest of the code as is, the output is

enter image description here

On a completely different note, perhaps a groupplot environment would be useful here. In the code below I use a gamma distribution (definition borrowed from Plotting a distribution with tikz package) to approximate the non-neutral cases. It's a bit of a hack job though. Don't have time to add much explanation at the moment, can do so later if needed.

I also added an example of how you can do a sketch without any plotting, just using bezier curves.

\documentclass[border=5mm,tikz]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.13}


\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;},
    declare function={gammapdf(\x,\k,\theta) = 1/(\theta^\k)*1/(gamma(\k))*\x^(\k-1)*exp(-\x/\theta);},
    declare function={gauss(\x,\mu,\sig)=exp(-((\x-\mu)^2)/(2*\sig^2))/(\sig*sqrt(2*pi));}
]
\begin{groupplot}[
  group style={
    group size=1 by 4,
    vertical sep=0pt,
    group name=G},
  xtick=\empty,ytick=\empty,
  width=8cm,height=3cm,
  axis lines=middle,
  y axis line style={draw=none},
  ymax=0.5,
  clip=false,
  no markers,
  domain=0:8,
  samples=100]

\nextgroupplot[
    xlabel=$x$,
    x label style={right,font=\large,at={(rel axis cs:0,0)}},
    x axis line style={stealth-},
    x post scale=-1]
\addplot {gammapdf(x,1.6,1.2)};

\nextgroupplot
\addplot {gammapdf(x,1.6,1.2)};
\nextgroupplot
\addplot+ [domain=-8:8] {gauss(x,0,1)};
\node [font=\tiny] at (0,0.1) {neutral};
\nextgroupplot[xmin=0,xmax=8]
\addplot+ [domain=0:4] {gammapdf(x,1.6,1.2)};
\end{groupplot}

\begin{axis}[width=8cm,height=3cm,ymax=0.3,at={(G c1r4.south west)},x post scale=-1,hide axis,xmin=0,xmax=8,ymax=0.5,enlargelimits=false,no markers]
\addplot+ [domain=0:4] {gammapdf(x,1.6,1.2)};
\end{axis}


\node [left,font=\large] at (G c1r1.south west) {$1$};
\node [left,font=\large] at (G c1r2.south west) {$2$};
\node [left,font=\large] at (G c1r3.south west) {$3$};
\node [left,font=\large] at (G c1r4.south west) {$4$};

\draw [-stealth] (G c1r4.south) -- ([yshift=0.3cm]G c1r1.north);
\fill (G c1r4.south) circle[radius=2pt];
\end{tikzpicture}

\begin{tikzpicture}[y=1.5cm]
\foreach [count=\i] \y in {4,3,2,1}
   \draw (0,\y) node [left,font=\large]{$\i$} -- (6,\y );
\node [right] at (6,4) {$x$};

\draw (0,3.05) to[out=0,in=180] ++(1.2,0.7) .. controls +(1,0) and  +(-4.5,0) .. ++(4.7,-0.65);
\draw (0,2.05) .. controls +(3,0) and  +(-0.7,0) .. (3,2.65) .. controls +(0.7,0) and +(-3,0) .. (6,2.05);
\draw (0,1.05) .. controls +(.7,0) and +(-.7,0)  .. (1.2,1.7) 
               .. controls +(0.5,0) and  +(-1.7,0) .. (3,1.1)
               .. controls +(1.7,0) and  +(-0.5,0) .. (4.8,1.7)
               .. controls +(0.7,0) and  +(-.7,0) .. (6,1.05);
\draw (6,4.05) to[out=180,in=0] ++(-1.2,0.7) .. controls +(-1,0) and  +(4.5,0) .. ++(-4.7,-0.65);

\fill (3,1) circle[radius=2pt];
\draw [-stealth] (3,1) -- (3,5);
\end{tikzpicture}
\end{document}

enter image description here

enter image description here

  • Thank you very much! I am trying to make the second distribution function, by adding \newcommand\chii[1]{(x^(#1/2-1)*exp((-x)/2))/(2^(#1/2)*(gamma(#1/2-1)))}. It seems like, TiKZ doesn't know the gamma function.. Do you know how I can plot function in case 2? – lala_12 Apr 17 '16 at 16:21
  • @em12 I updated my answer, perhaps it's helpful. – Torbjørn T. Apr 17 '16 at 19:43

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.