5

I tried to plot a sequence with pgfplots but I cannot set the color:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\begin{document}


\begin{tikzpicture}
  \pgfmathsetmacro{\Start}{1} %% start value
  \pgfmathsetmacro{\End}{5} %% end value
  \begin{axis}[mark options={mark=x},only marks,axis lines=center,xlabel=$n$,ylabel=$a_n$,samples at={\Start,...,\End},xtick={0,1,...,\End},grid=both, minor y tick num=4,enlargelimits]
    \addplot[green] coordinates {(0,0)};
    \addplot[green] {1/x};%
  \end{axis}
\end{tikzpicture}

\end{document}

What would be the proper way to specify the color here?

6

You have to use \addplot+ instead of \addplot. The \addplot version overwrites the default settings with the given options (here only green) and hence the plot is not drawn at all.On the other hand \addplot+ appends the options to the default ones.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}


\begin{tikzpicture}
  \pgfmathsetmacro{\Start}{1} %% start value
  \pgfmathsetmacro{\End}{5} %% end value
  \begin{axis}[mark options={mark=x},only marks,axis lines=center,xlabel=$n$,ylabel=$a_n$,samples at={\Start,...,\End},xtick={0,1,...,\End},grid=both, minor y tick num=4,enlargelimits]
    \addplot+[green] coordinates {(0,0)};
    \addplot+[green] {1/x};%
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

| improve this answer | |
  • Thanks. That works. But I don't understand why \addplot doesn't. If it overwrites the default settings, then giving no optional argument to it should set the options to nothing (which doesn't seem to be the case). Also it should be possible to write something like \addplot[draw,green] to make it work again. Can you explain this point a bit mor in detail? – Julia Sep 10 '15 at 9:45
  • 2
    @Julia It is rather \addplot[] that overwrites the defaults. A standalone \addplot doesn't do this. Additional [] clears up the existing style. A plus sign adds to the existing style. – percusse Sep 10 '15 at 10:04
  • @percusse. Ok. What would be the correct version of what I intended with \addplot[draw,green]? – Julia Sep 10 '15 at 10:39
  • 1
    @Julia \addplot[green,mark=*] {1/x}; – LaRiFaRi Sep 10 '15 at 11:37

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.