4

I have a coordinate system and need to plot linear functions in this coordinate system. The problem is that the functions only get plotted from x=-5 to x=5, although I set xmin=-11 and xmax=11. A way to resolve the plotting problem is to leave out xmin and xmax, but that messes up my coordinate system.

How can I make the plots go from xmin to xmax, while leaving the coordinate system as it is?

My code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}[
xmin=-11,xmax=11,
ymin=-11,ymax=11,
grid=both,
grid style={line width=.1pt, draw=darkgray!10},
major grid style={line width=.2pt,draw=darkgray!50},
axis lines=middle,
minor tick num=4,
enlargelimits={abs=0.5},
axis line style={latex-latex},
samples=100,
]
\addplot[blue] (x,2*x+2);
\addplot[red] (x,0.5*x);
\end{axis}
\end{tikzpicture}

\end{document}

My output:

output only from x=-5 to x=5

1 Answer 1

6

You seem to be confusing the xmin/xmax keys, which set the boundaries of the window with the domain key, which is what you want.

enter image description here

Complement : default key values

  • as you noticed, the domain key has default value -5:5
  • if left unspecified, the x/y/z - min/max keys are computed to fit the data provided to \addplot. In your case, you therefore would not need to specify the xmin/xmax keys, only the domain one.

Code

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\begin{axis}
  [
    xmin=-11,xmax=11, % not needed if domain=-11:11,
    ymin=-11,ymax=11,
    grid=both,
    grid style={line width=.1pt, draw=darkgray!10},
    major grid style={line width=.2pt,draw=darkgray!50},
    axis lines=middle,
    minor tick num=4,
    enlargelimits={abs=0.5},
    axis line style={latex-latex},
    samples=100,% you only need 2 for lines
    domain = -11:11,
  ]
  \addplot[blue] (x,2*x+2);
  \addplot[red] (x,0.5*x);
\end{axis}
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .