7

I have the following code.

\begin{figure}[p]
    \centering \begin{tikzpicture}
    \begin{axis}[
    use units,
        width=15cm, height=8cm,     % size of the image
        grid = major,
        grid style={dashed, gray!30},
        %xmode=log,log basis x=10,
        %ymode=log,log basis y=10,
        xmin=0,     % start the diagram at this x-coordinate
        ymin=0,     % start the diagram at this y-coordinate
        %/pgfplots/xtick={0,5,...,60}, % make steps of length 5
        %extra x ticks={23},
        %extra y ticks={0.507297},
        axis background/.style={fill=white},
        ylabel=Time,
        xlabel=Number of Datapoints (n),
        y unit=s,y unit prefix=m,
        tick align=outside]

      % import the correct data from a CSV file
      \addplot table [col sep=comma,trim cells=true,y=user] {datasets/mergesort.csv};

    \end{axis}  \end{tikzpicture}
    \caption{Mergesort Run Time for Sorting Random Data}
    \label{fig:merge_sort_from_csv} \end{figure}

What I get is a chart in which each point is represented with a blue dot and a blue line that connects those points.

If I change the line

\addplot table [col sep=comma,trim cells=true,y=user] {datasets/mergesort.csv};

to

\addplot[red] table [col sep=comma,trim cells=true,y=user] {datasets/mergesort.csv};

What I get is a straight red line without the points.

How I can get the same as before (both line and points) but in a red color?

  • Use [red,mark=*]. – Paul Gessler Jul 5 '14 at 18:31
  • Perfect @PaulGessler. Thanks a lot. It solved my problem. Do you also know any list with colors or symbols that I can use when creating a line chart? – Jim Blum Jul 5 '14 at 18:49
  • 2
    Have a look at Section 4.7 of the current (v1.10) manual. There are also pre-defined cycle lists described (with examples) in Section 4.7.7. – Paul Gessler Jul 5 '14 at 18:52
16

In the absence of any style keys for the \addplot commands, the default cycle list (color, as documented in Section 4.7.7 of the v1.11 pgfplots manual) is used:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:1]
  \addplot {x};
  \addplot {x^2};
  \addplot {x^3};
  \addplot {x^4};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

As soon as any style keys are specified, as in \addplot[red] <...>;, the default cycle list is no longer used for that plot (but observe that the plot's "spot" in the cycle list is still "consumed", as shown in the example):

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:1]
  \addplot[red] {x}; % added [red] here
  \addplot {x^2};
  \addplot {x^3};
  \addplot {x^4};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

The alternative plot command \addplot+ allows the user to append style options without overriding all styles set by the cycle list. For example:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:1]
  \addplot+[red] {x};
  \addplot {x^2};
  \addplot {x^3};
  \addplot {x^4};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

So, for the desired plot style (red with filled circular marks of the same color), you can use either approach:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:1]
  \addplot[red,mark=*] {x}; % red and filled circular marker, OR
  \addplot+[red,mark=*,mark options={fill=red}] {x^2}; % override cycle list style
  \addplot {x^3};
  \addplot {x^4};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

As to your request in a comment, the complete lists of markers and linestyles available in pgfplots are in Section 4.7 of the manual. Section 4.7.1 lists the markers and available options for styling them, while Section 4.7.2 does the same for line styles.

Rather than specifying the style for every individual plot command, it is also possible to use user-defined or pre-defined cycle lists to set the style for each successive plot command. The default cycle list was shown in the first sample. Here is one other pre-defined cycle list, exotic:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:1,cycle list name=exotic]
  \addplot {x};
  \addplot {x^2};
  \addplot {x^3};
  \addplot {x^4};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Other choices for pre-defined cycle lists are documented in Section 4.7.7 of the manual, and the second half of that section describes the syntax for defining your own cycle lists. A user-defined cycle list could be defined once per document and used for all plots in that document. This way, if you change your mind about the style, you only need to update the definition once and all plots will update accordingly.

  • You're quite welcome Jim! Please feel free to leave a comment here if there's anything else you'd like explained. – Paul Gessler Sep 11 '14 at 0:45
  • 1
    answers should always be like this one – SAAD Jul 5 '15 at 15:39

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.