Pgfplots 仅使用模式中的点 ( dashdotted
, dashdotdotted
)。如果我使用标记选项,标记似乎与线条的其余部分不兼容。是否可以使用star
(适当大小) 或square
等代替dot
。
它像是
dashdotted: -.-.-
dashdotdotted: -..-..-..
dashstar: -*-*-*
dashstarstar: -**-**-**
答案1
正如评论中提到的,这里可能可以使用装饰markings
。下面我定义了几个示例样式,其中一个借鉴了 marmot 对你的问题的评论。自定义dash pattern
用于获取已知虚线之间的距离,装饰用于将标记放置在间隙中。
它们可能无法在所有绘图上正常工作,至少在我的计算机上,它无法与选项smooth
(尺寸过大错误)配合使用,并且必须根据特定标记大小进行定制。但它可能很有用。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{decorations.markings}
\pgfplotsset{compat=1.16}
\tikzset{
dashstar/.style={
dash pattern=on 5pt off 5pt,
postaction={
decorate,
decoration={
markings,
mark=between positions 7.5pt and 1 step 10pt with {
\node {\pgfuseplotmark{star}};
}
}
}
},
dashstarstar/.style={ % from marmot's comments
dash pattern=on 5pt off 10pt,
postaction={
decorate,
decoration={
markings,
mark=between positions 10pt and 1
step 15pt
with {
\node at (-2pt,0pt) {\pgfuseplotmark{star}};
\node at (2pt,0pt) {\pgfuseplotmark{star}};
}
}
}
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot [dashstar,samples=200] {sin(x*180/pi)};
\addplot [dashstarstar] {1.1};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
以下是对Torbjørn T.'很好的答案适用于smooth
大量样本,但不能同时适用于两者。不同之处在于装饰是在较低级别实现的(至少我相信是这样)。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{decorations}
\pgfdeclaredecoration{stars}{initial}{\state{initial}[width=6pt,next state=star1]
{
}
\state{star1}[width=4pt,next state=gap]
{
\pgfuseplotmark{star}
}
\state{gap}[width=4pt,next state=star1]
{
}
\state{final}
{
\pgfpathmoveto{\pgfpointdecoratedpathlast}
}
}
\pgfdeclaredecoration{starstars}{initial}{\state{initial}[width=6pt,next state=star1]
{
}
\state{star1}[width=4pt,next state=star2]
{
\pgfuseplotmark{star}
}
\state{star2}[width=4pt,next state=gap]
{
\pgfuseplotmark{star}
}
\state{gap}[width=4pt,next state=star1]
{
}
\state{final}
{
\pgfpathmoveto{\pgfpointdecoratedpathlast}
}
}
\tikzset{
dashStar/.style={dash pattern=on 4pt off 4pt,postaction={decorate,decoration=stars}},
dashStarStar/.style={dash pattern=on 4pt off 8pt,postaction={decorate,decoration=starstars}},
}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[dashStar,samples=100] {sin(x*180/pi)};
\addplot[dashStarStar,smooth] {cos(x*720/pi)};
\legend{$\sin(x)$,$\cos(x)$}
\end{axis}
\end{tikzpicture}
\end{document}