我搜索了 pgfplots 生成的不同图表,但找不到我想要的样式,如下所示:我知道如何隐藏轴,但想知道 PGFplots 是否有自动生成比例参考线的选项(见图像的东南部)
答案1
您可以after end axis/.code
在完成其他所有操作后,使用它来执行一些绘图代码,并axis direction cs:
根据轴单位定义线条。
这里有一个键名为scale reference
,用于绘制如您所展示的标尺参考。您可以使用 自定义条形的长度scale reference x length
,使用 自定义线条样式和位置scale reference style
,使用 自定义标签scale reference x unit
。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\pgfplotsset{
compat=newest,
plot coordinates/math parser=false,
every axis/.append style={
tick label style={font=\scriptsize},
label style={font=\scriptsize}
}
}
\pgfplotsset{
scale reference style/.style={
every scale reference/.append style={#1}
},
every scale reference/.style={
shift={(rel axis cs:0,0)},
thick
},
scale reference/x unit/.initial={},
scale reference/y unit/.initial={},
scale reference/x length/.initial=1,
scale reference/y length/.initial=1,
scale reference/.style={
/pgfplots/scale reference/.cd,
#1,
/pgfplots/after end axis/.code={
\draw [/pgfplots/every scale reference] (0,0) --
++(axis direction cs:\pgfkeysvalueof{/pgfplots/scale reference/x length},0)
node [/pgfplots/every tick label, pos=0.5, below] {\pgfkeysvalueof{/pgfplots/scale reference/x length} \pgfkeysvalueof{/pgfplots/scale reference/x unit}}
-- ++(0,5pt);
\draw [/pgfplots/every scale reference] (0,0) -- ++(axis direction cs:0,\pgfkeysvalueof{/pgfplots/scale reference/y length}) node [/pgfplots/every tick label, pos=0.5, left, ] {\pgfkeysvalueof{/pgfplots/scale reference/y length}\pgfkeysvalueof{/pgfplots/scale reference/y unit}} -- ++(5pt,0);
}
}
}
\begin{tikzpicture}
\begin{axis}[
domain=0:pi,
width=10cm, height=4cm,
enlargelimits=false,
hide axis,
scale reference={
x unit=secs,
y unit=$\mu$V,
x length=0.5,
y length=50
},
scale reference style={shift={(rel axis cs:0,-0.1)}}
]
\addplot [samples=500] {50*sin(deg(x*20))*sin(deg(x*20/7*rnd/2))+rnd};
\end{axis}
\end{tikzpicture}
\end{document}