7

When I try to plot a function on a small range, the output is an empty graph from 0 to 1 on the x and y axis. What can I do to fix this? My tex looks like this:

\begin{tikzpicture}
\begin{axis}[
xmin=4.0,
xmax=4.001,
samples=60,
xlabel=$time$,
ylabel={$acceleration$}
] 

\addplot [very thick, blue] {5*x-x^2}; 
\end{axis}
\end{tikzpicture}
5

enter image description here

You can rescale the calculation:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
xmin=0,
xmax=1,
samples=60,
xlabel=$time$,
xticklabel=\pgfmathparse{4+\tick/1000}\pgfmathresult,
ylabel={$acceleration$}
] 

\addplot [very thick, blue] {5*(4+x/1000)-(4+x/1000)^2}; 
\end{axis}
\end{tikzpicture}

\end{document}
3
  • How does this line work? xticklabel=\pgfmathparse{4+\tick/1000}\pgfmathresult
    – Greg
    May 16 '13 at 0:29
  • @user2116192 as far as I understand it (which isn't far, I've never used tikz apart from the occasional answer here) \pgfmathparse is the general utility function in pgf to evaluate an expression, leaving its result in \pgfmathresult in this context \tick is the value going from 0 to 1 which would be the default label, but by setting the xticklable key it comes out as 4.00.. instead. May 16 '13 at 0:34
  • 1
    It may be worth mentioning the library fixedpointarithmetic, which uses the fp package for calculations (9 digits before and 9 after the decimal point). May 16 '13 at 9:30
7

The problem here is that the function isn't evaluated within the axis range. The standard domain is -5:5 (this is not changed by setting xmin and xmax), so with 60 samples, the function is evaluated at 3.98 and 4.15. If you set the domain=4:4.001 (so equal to the visible range set using xmin and xmax), the data is plotted correctly without any manual coordinate transformations:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
compat=newest,
samples=60,
xmin=4.0, xmax=4.001,
domain=4:4.001,
xlabel=Time $t$,
ylabel=Acceleration $a$,
ticklabel style={ % Make sure all relevant digits are printed
    /pgf/number format/.cd,
        fixed,
        fixed zerofill,
        precision=4
}
] 

\addplot [very thick, blue] {5*x-x^2}; 
\end{axis}
\end{tikzpicture}
\end{document}
2
  • 1
    By the way, instead of $time$/$acceleration$ in the labels we should write \textit{...} if we want italic text or simply ... if we want it upright. Not the math mode.
    – Manuel
    May 21 '13 at 17:34
  • Thanks for the response. It shows that as $\Delta t$ shrinks to zero, the acceleration tends toward some value, which allows one to plot complicated motion with a few simple equations and a computer. Thanks again!
    – Greg
    May 21 '13 at 20:20

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.