Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'd like to plot the function 1/2*x(x+1) in for integer values ranging from 3 to 20 and in particular I'd like the function to be plotted in those points. I have managed to create the following graph so far which obviously is not what I want

Plotting 1/2*x(x+1)

this is the cose I have written:

\begin{figure}
\begin{tikzpicture}
  \begin{axis}[ 
    xlabel={Numero di elementi},
    ylabel={Numero di coppie},
    xmin=3, xmax=20,
    ymin=0, ymax=55,
    xtick={1,...,20}
  ] 
    \addplot {0.5*x*(x-1)}; 
  \end{axis}
\end{tikzpicture}
\end{figure}

so what is that I am doing wrong?

share|improve this question

3 Answers 3

up vote 5 down vote accepted

You can adjust the calculation/plotting range with the domain keyword. If you want the evaluated points only for integer values of x use the samples keyword to select the appropriate number of samples within the domain range:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ 
    xlabel={Numero di elementi},
    ylabel={Numero di coppie},
    xmin=3, xmax=20,
    ymin=0, %ymax=55,
    xtick={1,...,20},
    domain={3:20},
    samples=18
  ] 
    \addplot {0.5*x*(x-1)}; 
  \end{axis}
\end{tikzpicture}
\end{document}

Plot example

share|improve this answer

The other solutions here try to coerce pgfplots into choosing the samples at the desired locations. There is already a very explicit way to specify where you want the samples taken at and that is to use

samples at={<list>}

enter image description here

Notes:

  • The samples at= has a higher precedence than samples and domain. Hence, samples and domain are ignored when the samples at= is specified.
  • If you need to clear a previously set samples at= list, you can do so via samples at={}. This will re-activate the domain key (and also the samples key, but the documentation does not explicitly point this out).

Code:

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[ 
    xlabel={Numero di elementi},
    ylabel={Numero di coppie},
    xmin=3, xmax=20,
    ymin=0, ymax=55,
    xtick={1,...,20},
    samples at={3,...,20}
  ] 
    \addplot {0.5*x*(x-1)}; 
  \end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
    
Why do I keep forgetting this? :) –  percusse Nov 18 '12 at 20:38
    
@percusse: :-) Actually, I don't even know why I keep remembering this. I am pretty sure I have never used it until now, but for reason it is stuck in my head. –  Peter Grill Nov 18 '12 at 20:40

You could use \foreach command.

\begin{figure}
\begin{tikzpicture}
  \begin{axis}[ 
    xlabel={Numero di elementi},
    ylabel={Numero di coppie},
    xmin=3, xmax=20,
    ymin=0, ymax=55,
    xtick={1,...,20}
  ] 
  \foreach \n in {3,4,...,20}{
    \addplot [mark=*] coordinates {(\n,{\n*(\n-1)/2})}; 
    }
  \end{axis}
\end{tikzpicture}
\end{figure}

But I don't get why you say ymax=55 because then you only need to plot from 3 to 11 (the rest of points don't appear). If you want all the points to appear you should comment it.

Plot

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.