在绘图标题中包含绘图标记

在绘图标题中包含绘图标记

我想在图表标题中使用标记形式\usetikzlibrary{plotmarks},例如:pentagon*diamond*square*。它们在图表中起标记的作用,但当我想要将它们包含在标题中时出现错误(而不是中的“” )triangle*x\mathbf{}

enter image description here

这是我的代码:

\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*}有效,但也会改变标题。

.
.
.
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}$},

它看起来像这样:

enter image description here

使用后,\def\showmark#1{\tikz[baseline]\node[red]{\pgfuseplotmark{#1}};}y 对齐的问题得到解决,但标题不再居中。

enter image description here

使用\showmark{square*}=$\SI{100}{\kilo\Hz}$Instead of\showmark{x}=$\SI{100}{\kilo\Hz}$会导致:

enter image description here

答案1

您可以使用\pgfuseplotmark{name_of_mark}来显示markers并将其包含在这样的节点中

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

您可以使用以下方式修改垂直对齐方式baseline=<yshift>

完整代码

\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}    

更新

标记+x定义pgflibraryplothandlers.code.tex如下

\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
} 

如果您想修复或标记,则需要%在每一行代码的末尾添加(我不知道这是否是一个错误或者有避免的理由)。+xpgf%

最终代码

\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

相关内容