0

I have made this pgfplot but not able to remove yaxis negative part. I want yaxis to start from 0 above.

The complete running code and picture is attached. Kindly guide me in this regard.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage{amsmath}

\definecolor{babyblueeyes}{rgb}{0.63, 0.79, 0.95}
\definecolor{blizzardblue}{rgb}{0.67, 0.9, 0.93}

\begin{document}

\begin{center}
\begin{tikzpicture}[
  declare function={
    unifpdf(\x,\a,\b) = 1/(\b-\a);
  }]
  \begin{axis}[scale=2,
    width=7cm, height=6cm,
    axis x line  = bottom,
    axis x line shift=-0.008,
    axis y line  = left,
    samples=10, 
    xmin=0,
    xmax=35,
    no marks,
    scaled ticks=false,
    tick label style={/pgf/number format/fixed},
    xlabel=$x$,
    xtick = {12,30},
    xticklabels = {$5$,$20$},
    xlabel style={xshift=18pt, yshift=34pt, at={(1,0)}, anchor=east},
    ylabel style={rotate=-90, at={(0,1)}, anchor=south},
    ymin=-0.008,
    ymax=0.05, 
    ytick ={0,1/30},
    yticklabels={$0$,$\dfrac{1}{20}$},
    ylabel=$f_X(x)$]

    \addplot[ultra thick, smooth, domain=0:30, color=babyblueeyes]
    {unifpdf(x,0,30)};

    \addplot[ultra thick, ycomb, mark=o, samples at={12,30}, dashed, color=blue]{unifpdf(x,0,30)};

    \addplot[draw=none, smooth, thick, domain=0:12, fill=babyblueeyes, samples=30]{unifpdf(x,0,30)} \closedcycle;

    \node at (axis cs:6,0.017) {$A$};

    \draw [latex-latex](axis cs:3,0.0065) -- node [fill=babyblueeyes] {$W$} (axis cs:12,0.0065);

    \draw [ultra thick,blue,dashed] (3,0) -- (3,0.0099);

    \filldraw (3,0) circle (0.08cm);
    % \draw[very thick, {Stealth}-{Stealth}] (3,0.0065) -- (12,0.0065);

    \addplot[draw=none, smooth, thick, domain=12.05:30.05, fill=blizzardblue, samples=30]{unifpdf(x,0,30)} \closedcycle;

    \draw [latex-latex](axis cs:18,0.0065) -- node [fill=blizzardblue] {$W$} (axis cs:30,0.0065);

    \filldraw (18,0) circle (0.08cm);

    \draw [ultra thick,blue,dashed] (18,0) -- (18,0.0099);

    \node at (axis cs:21,0.017) {$A^c$};

    \node at (12,0) [below, yshift=-14pt] {Train at 7:15};

    \node at (30,0) [below, yshift=-14pt] {Train at 7:30};

  \end{axis}

\end{tikzpicture}
\end{center}


\end{document}

enter image description here

0

You set ymin to a negative value. One way of achieving what you want is to set ymin to 0 and to add clip=false to make sure that the extra stuff does not get clipped away.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\usepackage{amsmath}

\definecolor{babyblueeyes}{rgb}{0.63, 0.79, 0.95}
\definecolor{blizzardblue}{rgb}{0.67, 0.9, 0.93}

\begin{document}

\begin{center}
\begin{tikzpicture}[
  declare function={
    unifpdf(\x,\a,\b) = 1/(\b-\a);
  }]
  \begin{axis}[scale=2,
    width=6.8cm, height=6cm,
    axis x line  = bottom,
    axis x line shift=-0.008,
    axis y line  = left,
    samples=10, 
    xmin=0,
    xmax=35,
    no marks,
    scaled ticks=false,
    tick label style={/pgf/number format/fixed},
    xlabel=$x$,
    xtick = {12,30},
    xticklabels = {$5$,$20$},
    xlabel style={xshift=18pt, yshift=34pt, at={(1,0)}, anchor=east},
    ylabel style={rotate=-90, at={(0,1)}, anchor=south},
    ymin=0,
    ymax=0.05, 
    ytick ={0,1/30},
    yticklabels={$0$,$\dfrac{1}{20}$},
    ylabel=$f_X(x)$,
    clip=false]

    \addplot[ultra thick, smooth, domain=0:30, color=babyblueeyes]
    {unifpdf(x,0,30)};

    \addplot[ultra thick, ycomb, mark=o, samples at={12,30}, dashed, color=blue]{unifpdf(x,0,30)};

    \addplot[draw=none, smooth, thick, domain=0:12, fill=babyblueeyes, samples=30]{unifpdf(x,0,30)} \closedcycle;

    \node at (axis cs:6,0.017) {$A$};

    \draw [latex-latex](axis cs:3,0.0065) -- node [fill=babyblueeyes] {$W$} (axis cs:12,0.0065);

    \draw [ultra thick,blue,dashed] (3,0) -- (3,0.0099);

    \filldraw (3,0) circle[radius=0.08cm];
    % \draw[very thick, {Stealth}-{Stealth}] (3,0.0065) -- (12,0.0065);

    \addplot[draw=none, smooth, thick, domain=12.05:30.05, fill=blizzardblue, samples=30]{unifpdf(x,0,30)} \closedcycle;

    \draw [latex-latex](axis cs:18,0.0065) -- node [fill=blizzardblue] {$W$} (axis cs:30,0.0065);

    \filldraw (18,0) circle[radius=0.08cm];

    \draw [ultra thick,blue,dashed] (18,0) -- (18,0.0099);

    \node at (axis cs:21,0.017) {$A^c$};

    \node at (12,0) [below, yshift=-14pt] {Train at 7:15};

    \node at (30,0) [below, yshift=-14pt] {Train at 7:30};

  \end{axis}

\end{tikzpicture}
\end{center}
\end{document}

enter image description here

Note that this is a "minimal damage" solution. One could just draw the extra stuff outside of the axis using symbolic coordinates as references. Also, you do not need all these axis cs: if you use a reasonably new version of pgfplots.

| improve this answer | |
  • Thank you very much Schrodinger's cat, technically you are my guide in this pursuit of learning pgfplots. – Raja Ali Riaz May 4 at 15:32

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.