pgfplots 中的标记锚点

pgfplots 中的标记锚点

我如何修改标记(本例中为三角形)的锚点,使其位于三角形的最左端?现在,很明显锚点是三角形的中心。

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{tikzpicture}

\begin{axis}[xmin=0, xmax=2.8,clip=false,
    ymin=0.0, ymax=0.3]


\addplot [mark=triangle*, mark size=2, mark options={solid,draw=black,fill=black}, only marks, every mark/.append style={rotate=90}]
table {%
0.0 0.0
};

\end{axis}

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

最简单的方法可能就是添加xshift=\pgfplotmarksize+\pgflinewidthmark options

另一种方法可能是定义一个新的 plotmark,如mytri下面的代码所示。

xshift左、triangle*mytri

在此处输入图片描述

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\pgfdeclareplotmark{mytri}
{%
  \pgfpathmoveto{\pgfqpoint{0pt}{-1\pgflinewidth}}%
  \pgfpathlineto{\pgfqpointpolar{-60}{2\pgfplotmarksize}}%
  \pgfpathlineto{\pgfqpointpolar{-120}{2\pgfplotmarksize}}%
  \pgfpathclose
  \pgfusepathqfillstroke
}


\begin{document}
\begin{tikzpicture}

\begin{axis}[xmin=0, xmax=2.8,clip=false,
    ymin=0.0, ymax=0.3,grid]

\addplot [mark=mytri,
          mark size=2,
          mark options={
              draw=black,
              fill=black
          },
          only marks,
          every mark/.append style={rotate=90}
]
table {%
1 0.1
};
\addplot [mark=triangle*,
          mark size=2,
          mark options={
              draw=black,
              fill=black,
              xshift=2.4pt % added
          },
          only marks,
          every mark/.append style={rotate=90}
]
table {%
0.5 0.1
};

\end{axis}

\end{tikzpicture}
\end{document}

还有一些其他(不太完美)的定义。默认定义triangle*用红色绘制在顶部,带有opacity=0.5,因此可以看到差异。不过我不知道如何使其独立于标记大小。

在此处输入图片描述

\RequirePackage{luatex85}
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\pgfdeclareplotmark{mytri}
{%
  \pgfpathmoveto{\pgfqpoint{0pt}{-\pgflinewidth}}%
  \pgfpathlineto{\pgfpointadd{\pgfqpointpolar{-60}{2\pgfplotmarksize}}{\pgfpoint{-0.5\pgflinewidth}{0pt}}}%
  \pgfpathlineto{\pgfpointadd{\pgfqpointpolar{-120}{2\pgfplotmarksize}}{\pgfpoint{0.5\pgflinewidth}{0pt}}}%
  \pgfpathclose
  \pgfusepathqfillstroke
}

\pgfdeclareplotmark{mytri2}
{%
  \pgfpathmoveto{\pgfqpoint{0pt}{0pt}}%
  \pgfpathlineto{\pgfqpointpolar{-60}{2\pgfplotmarksize}}%
  \pgfpathlineto{\pgfqpointpolar{-120}{2\pgfplotmarksize}}%
  \pgfpathclose
  \pgfusepathqfill % only fill, no stroke
}


\begin{document}
\begin{tikzpicture}[
  % change this to change the mark size
  declare function={msize=2;}
]

\begin{axis}[
  width=2cm,height=1cm,
  scale only axis,
  xmin=0.45,xmax=0.65,
  ymin=0.0, ymax=0.2,
  xtick={0.5,0.6},
  xticklabels={},
  ytick=\empty,
  grid,
  clip=false
]


\addplot [mark=mytri,
          mark size=msize,
          mark options={
              draw=black,
              fill=black,
          },
          only marks,
          every mark/.append style={rotate=90},
          nodes near coords={\tiny mytri},
]
table {%
0.5 0.1
};
%
\addplot [mark=mytri2,
          mark size=msize,
          mark options={
              draw=black,
              fill=black,
          },
          only marks,
          every mark/.append style={rotate=90},
          nodes near coords={\tiny mytri2},
          nodes near coords align=below
]
table {%
0.6 0.1
};


\addplot [mark=triangle*,
          mark size=msize,
          mark options={
              draw=red,
              fill=red,
              xshift={msize+0.4pt},
              draw opacity=0.5 % to see difference with the other two 
          },
          only marks,
          every mark/.append style={rotate=90}
]
table {%
0.5 0.1
0.6 0.1
};

\end{axis}

\end{tikzpicture}
\end{document}

相关内容