3

I want to use markers form \usetikzlibrary{plotmarks}, like: pentagon*, diamond*, square* and triangle* in the title of the plot. They work as markers in the plot but i get an error when in want to include them in the title (instead of the "x" in \mathbf{})

enter image description here

This is my code:

\begin{filecontents*}{data.txt}
b d c
1 a 2   
2 b 4   
3 c 6   
4 d 8   
5 e 10  
6 f 12  
7 g 14  
8 x 16  
9 x 18  
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage{siunitx}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{lipsum}

\pgfplotscreateplotcyclelist{mycolorlist}{
blue!01!green,every mark/.append style={fill=blue!10!black},mark=+\\
}

\begin{document}
\begin{figure}
  \begin{tikzpicture}
    \begin{axis}[
        grid=both,
        width=12cm, 
        height=9cm,
        xtick pos=left,
        ytick pos=left,
        title style={draw,fill=none,name=legend},
        title={%
            ${\color{red}\mathbf{x}}=\SI{100}{\kilo\Hz}$,
            ${\color{red}\mathbf{x}}=\SI{10}{\kilo\Hz}$,
            ${\color{red}\mathbf{x}}=\SI{1}{\kilo\Hz}$,
            ${\color{red}\mathbf{x}}=\SI{100}{\Hz}$,
            ${\color{red}\mathbf{x}}=\SI{10}{\Hz}$,
            ${\color{red}\mathbf{x}}=\SI{1}{\Hz}$},
        xlabel={R}, ylabel={Z}, 
        legend style={at={(0.02,0.98)},anchor=north west,cells={anchor=west}},
        legend style={font=\footnotesize},
        cycle list name=mycolorlist,
        scatter,
        point meta=explicit symbolic,
        scatter/classes={
          x={},% empty argument means: use the default style       
          g={mark=o,red!10!black},
          f={mark=square*,red!20!black},
          e={mark=diamond*,red!30!black},
          d={mark=triangle*,red!40!black},
          c={mark=pentagon*,red!50!black},
          b={mark=*,red!60!black},
          a={mark=oplus,red!70!black},
          z={mark=*,red!80!black}
          },
        ]
        \addplot table [x=b,y=c, meta=d]{data.txt};

        \legend{Z}
   \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

\pgfuseplotmark{square*} works but it also changes the title.

.
.
.
title style={draw,fill=none,name=legend},
        title={%
            ${\color{red}\pgfuseplotmark{o}}=\SI{100}{\kilo\Hz}$,
            ${\color{red}\pgfuseplotmark{square*}}=\SI{10}{\kilo\Hz}$,
            ${\color{red}\pgfuseplotmark{diamond*}}=\SI{1}{\kilo\Hz}$,
            ${\color{red}\pgfuseplotmark{triangle*}}=\SI{100}{\Hz}$,
            ${\color{red}\pgfuseplotmark{pentagon*}}=\SI{10}{\Hz}$,
            ${\color{red}\pgfuseplotmark{*}}=\SI{1}{\Hz}$,
            ${\color{red}\pgfuseplotmark{oplus}}=\SI{20}{\mHz}$},

It looks like this:

enter image description here

After using \def\showmark#1{\tikz[baseline]\node[red]{\pgfuseplotmark{#1}};} the problem with the y-alignment is fixed, but the title is not centered anymore.

enter image description here

Using \showmark{square*}=$\SI{100}{\kilo\Hz}$ instead of \showmark{x}=$\SI{100}{\kilo\Hz}$ leads to:

enter image description here

Björn
  • 327
  • 1
    What error do you get? \pgfuseplotmark{square*} (cf. e.g. http://tex.stackexchange.com/questions/64567) works here, though the vertical alignment is off. – Torbjørn T. Jan 11 '17 at 11:54
  • the error is: '! Undefined control sequence.' – Björn Jan 11 '17 at 12:03
  • 1
    Can you edit the code to show what you tried, and also state the whole error (from the ! before Undefined to the ? a couple of lines later), so that we can see which macro is undefined. – Torbjørn T. Jan 11 '17 at 12:06
  • Right, so it works, but as I mentioned the vertical alignment (of the plotmarks) is off. As is the spacing before the = I see. I don't have a fix at the moment, but I'm sure someone does. – Torbjørn T. Jan 11 '17 at 12:15
  • Answer updated. – Salim Bou Jan 11 '17 at 17:17

1 Answers1

2

You can use \pgfuseplotmark{name_of_mark} to show markers and you can include it within a node like this

\def\showmark#1{\tikz[baseline]\node[red]{\pgfuseplotmark{#1}};}

You can modify vertical alignment with baseline=<yshift>

Complete Code

\begin{filecontents*}{data.txt}
b d c
1 a 2   
2 b 4   
3 c 6   
4 d 8   
5 e 10  
6 f 12  
7 g 14  
8 x 16  
9 x 18  
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage{siunitx}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{lipsum}

\pgfplotscreateplotcyclelist{mycolorlist}{
blue!01!green,every mark/.append style={fill=blue!10!black},mark=+\\
}

\def\showmark#1{\tikz\node[red]{\pgfuseplotmark{#1}};}

\begin{document}
\begin{figure}
  \begin{tikzpicture}
    \begin{axis}[
        grid=both,
        width=12cm, 
        height=9cm,
        xtick pos=left,
        ytick pos=left,
        legend style={draw,fill=none,name=legend},
        title={%
            \showmark{x}=$\SI{100}{\kilo\Hz}$,
            \showmark{o}=$\SI{10}{\kilo\Hz}$,
            \showmark{square*}=$\SI{1}{\kilo\Hz}$,
            \showmark{diamond*}=$\SI{100}{\Hz}$,
            \showmark{triangle*}=$\SI{10}{\Hz}$,
            \showmark{pentagon*}=$\SI{1}{\Hz}$},
        xlabel={R}, ylabel={Z}, 
        legend style={at={(0.02,0.98)},anchor=north west,cells={anchor=west}},
        legend style={font=\footnotesize},
        cycle list name=mycolorlist,
        scatter,
        point meta=explicit symbolic,
        scatter/classes={
          x={},% empty argument means: use the default style       
          g={mark=o,red!10!black},
          f={mark=square*,red!20!black},
          e={mark=diamond*,red!30!black},
          d={mark=triangle*,red!40!black},
          c={mark=pentagon*,red!50!black},
          b={mark=*,red!60!black},
          a={mark=oplus,red!70!black},
          z={mark=*,red!80!black}
          },
        ]
        \addplot table [x=b,y=c, meta=d]{data.txt};

        \legend{Z}
   \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}    

Update

markers + and x are defined in pgflibraryplothandlers.code.tex like this

\pgfdeclareplotmark{+}
{%
  \pgfpathmoveto{\pgfqpoint{-\pgfplotmarksize}{0pt}}
  \pgfpathlineto{\pgfqpoint{\pgfplotmarksize}{0pt}}
  \pgfpathmoveto{\pgfqpoint{0pt}{\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpoint{0pt}{-\pgfplotmarksize}}
  \pgfusepathqstroke
}

\pgfdeclareplotmark{x}
{%
  \pgfpathmoveto{\pgfqpoint{-.70710678\pgfplotmarksize}{-.70710678\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpoint{.70710678\pgfplotmarksize}{.70710678\pgfplotmarksize}}
  \pgfpathmoveto{\pgfqpoint{-.70710678\pgfplotmarksize}{.70710678\pgfplotmarksize}}
  \pgfpathlineto{\pgfqpoint{.70710678\pgfplotmarksize}{-.70710678\pgfplotmarksize}}
  \pgfusepathqstroke
} 

You need to add % in the end of every line code if you want to fix + or x marker ( I do not know if this is a bug in pgf or there is a reason for avoiding % ).

Final Code

\begin{filecontents*}{data.txt}
b d c
1 a 2   
2 b 4   
3 c 6   
4 d 8   
5 e 10  
6 f 12  
7 g 14  
8 x 16  
9 x 18  
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage{siunitx}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{lipsum}

\pgfplotscreateplotcyclelist{mycolorlist}{
blue!01!green,every mark/.append style={fill=blue!10!black},mark=+\\
}

% Fix x marker
\pgfdeclareplotmark{x}
{%
  \pgfpathmoveto{\pgfqpoint{-.70710678\pgfplotmarksize}{-.70710678\pgfplotmarksize}}%
  \pgfpathlineto{\pgfqpoint{.70710678\pgfplotmarksize}{.70710678\pgfplotmarksize}}%
  \pgfpathmoveto{\pgfqpoint{-.70710678\pgfplotmarksize}{.70710678\pgfplotmarksize}}%
  \pgfpathlineto{\pgfqpoint{.70710678\pgfplotmarksize}{-.70710678\pgfplotmarksize}}%
  \pgfusepathqstroke
}


\def\showmark#1{\tikz[baseline]\node[red]{\pgfuseplotmark{#1}};}

\begin{document}
\begin{figure}
  \begin{tikzpicture}
    \begin{axis}[
        grid=both,
        width=12cm, 
        height=9cm,
        xtick pos=left,
        ytick pos=left,
        legend style={draw,fill=none,name=legend},
        title style=draw,
        title={%
            \showmark{x}=$\SI{100}{\kilo\Hz}$,
            \showmark{x}=$\SI{10}{\kilo\Hz}$,
            \showmark{square*}=$\SI{1}{\kilo\Hz}$,
            \showmark{diamond*}=$\SI{100}{\Hz}$,
            \showmark{triangle*}=$\SI{10}{\Hz}$,
            \showmark{pentagon*}=$\SI{1}{\Hz}$},
        xlabel={R}, ylabel={Z}, 
        legend style={at={(0.02,0.98)},anchor=north west,cells={anchor=west}},
        legend style={font=\footnotesize},
        cycle list name=mycolorlist,
        scatter,
        point meta=explicit symbolic,
        scatter/classes={
          x={},% empty argument means: use the default style       
          g={mark=o,red!10!black},
          f={mark=square*,red!20!black},
          e={mark=diamond*,red!30!black},
          d={mark=triangle*,red!40!black},
          c={mark=pentagon*,red!50!black},
          b={mark=*,red!60!black},
          a={mark=oplus,red!70!black},
          z={mark=*,red!80!black}
          },
        ]
        \addplot table [x=b,y=c, meta=d]{data.txt};

        \legend{Z}
   \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

enter image description here

Salim Bou
  • 17,021
  • 2
  • 31
  • 76
  • 1
    See Björn's update on his question, there is some unwanted whitespace at the start of the title. – Torbjørn T. Jan 11 '17 at 12:58
  • I changed the markers. So, instead of the first ( \showmark{x}=$\SI{100}{\kilo\Hz}$) i tried \showmark{square*}=$\SI{100}{\kilo\Hz}$ and everything is centered again. – Björn Jan 11 '17 at 13:05
  • THANKS! works fine! I have a follow-up question on the legends, which I'll post soon. Would be great if somebody could link it to this one. – Björn Jan 12 '17 at 09:29