2

I have a problem with domain specification in addplot which collides with domain specification in axis.

The code below plots the x axis as well as the function (expo) from 0.1 to 2. My desired behavior is that the axis is plotted from 0 to 2 and only the function from 0.1 to 2.

\begin{figure}[!ht]
\centering

\pgfmathdeclarefunction{expo}{1}{%
    \pgfmathparse{1/(#1)*exp(-x/#1)}%
}

\begin{tikzpicture}
    \begin{axis}[
        height=5cm,width=10cm,no markers,samples=100,axis lines=left,domain=0:2,xtick={0,0.5,1,1.5},ytick={0,4,8},enlargelimits=false
    ]
        \addplot[domain=0.1:2,thick] {expo(0.1)} node[pos=0.7,pin=45:{$\dfrac{1}{\lambda}\approx\SI{0.1}{\meter}$}] {};
    \end{axis}
\end{tikzpicture}
\end{figure}

Does anybody know, how to obtain the desired behaviour?

  • It is always better to compose a fully compilable MWE including \documentclass and the appropriate packages that sets up the problem. – Peter Grill Feb 24 '14 at 18:13
3

You should use xmin=0, xmax=2 to specify the min/max of the axis:

enter image description here

Code:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{mathtools}
\usepackage{siunitx}

\pgfmathdeclarefunction{expo}{1}{%
    \pgfmathparse{1/(#1)*exp(-x/#1)}%
}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        height=5cm,width=10cm,
        no markers,
        samples=100,
        axis lines=left,
        xmin=0, xmax=2,%     <---- This controls the axis min/max
        xtick={0,0.5,1,1.5},
        ytick={0,4,8},
        enlargelimits=false,
    ]
        \addplot[domain=0.1:2,thick] {expo(0.1)} node[pos=0.7,pin=45:{$\dfrac{1}{\lambda}\approx\SI{0.1}{\meter}$}] {};
    \end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |
  • Many thanks! Unfortunately, I was unable to find it in the documentation of pgfplots: To me it seems, that it only describes the syntax of the axis environment as '\begin{axis}[options] ... \end{axis}'. However, there should be a list of the possible 'options'. Do you know if there is a way to find this systematically in the reference? – SeMa Feb 25 '14 at 19:08
  • They are documented in the pgfplots manual. Typing texdoc pgfplots at a Unix shell prompt should retrieve the manual for you. – Peter Grill Feb 25 '14 at 23:26

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.