我如何才能增强 xtick 标签的外观(将它们移至绘制线的顶部)。
\documentclass[border=5pt]{standalone}
\usepackage{tikz,pgfplots}
\usepackage{xcolor}
\definecolor{lava}{rgb}{0.81, 0.06, 0.13}
\definecolor{myblue}{rgb}{0.0, 0.30, 0.60}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$t$(s)},
grid=both,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
ylabel={$v(t)$},
axis lines=left,
scaled ticks=false,
axis lines=middle,
minor tick num=4,
enlargelimits={abs=0.2},
xmin = 0, xmax =8,
ymin = -0.5, ymax = 0.5,
ytick={0,0.5},
yticklabels={$0$,$V_m$},
xtick= { 0,1,2,3,4,5,6,7,8},
xticklabels={0,$T/2$,$T$,$3T/2$,$2T$,$5T/2$,$3T$,$7T/2$,$4T$},
x tick label style={font=\small, rotate=0,below}
]
\addplot[domain=0:1,samples=50
,smooth,lava, ultra thick] {0.5*x};
\addplot[domain=1:2,samples=50,smooth,lava, ultra thick] {0};
\addplot[domain=2:3,samples=50,smooth,lava, ultra thick] {-0.5*(x-2)};
\addplot[domain=3:4,samples=50,smooth,lava, ultra thick] {0};
\addplot[domain=4:5,samples=50,smooth,lava, ultra thick] {0.5*(x-4)};
\addplot[domain=5:6,samples=50,smooth,
lava, ultra thick] {0};
\addplot[domain=6:7,samples=50,smooth,lava, ultra thick] {-0.5*(x-6)};
\addplot[domain=7:8,samples=50,smooth,lava, ultra thick] {0};
\draw [ ultra thick, lava,dashed] (1,0) -- (1,0.5);
\draw [ ultra thick, lava,dashed] (3,0) -- (3,-0.5);
\draw [ ultra thick, lava,dashed] (5,0) -- (5,0.5);
\draw [ ultra thick, lava,dashed] (7,0) -- (7,-0.5);
\end{axis}
\end{tikzpicture}
\end{document}
答案1
默认情况下,图表的高度为\axisdefaultheight
。我将标签移动了该高度的一半:xticklabel style={yshift=.5*\axisdefaultheight}
更新 :
正如@Torbjørn T. 在其评论中所指出的,通过使用 pgf 键\pgfkeysvalueof{/pgfplots/height}
而不是 ,该解决方案变得更加通用\axisdefaultheight
。
xticklabel style={yshift=.5*\pgfkeysvalueof{/pgfplots/height}}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
%\usepackage{xcolor}
\definecolor{lava}{rgb}{0.81, 0.06, 0.13}
\definecolor{myblue}{rgb}{0.0, 0.30, 0.60}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel={$t$(s)},
grid=both,
grid style={line width=.1pt, draw=gray!10},
major grid style={line width=.2pt,draw=gray!50},
ylabel={$v(t)$},
axis lines=left,
scaled ticks=false,
axis lines=middle,
minor tick num=4,
enlargelimits={abs=0.2},
xmin = 0, xmax =8,
ymin = -0.5, ymax = 0.5,
ytick={0,0.5},
yticklabels={$0$,$V_m$},
xtick= { 0,1,2,3,4,5,6,7,8},
xticklabels={0,$T/2$,$T$,$3T/2$,$2T$,$5T/2$,$3T$,$7T/2$,$4T$},
x tick label style={font=\small, rotate=0,below},
xticklabel style={yshift=.5*\pgfkeysvalueof{/pgfplots/height}}
]
\addplot[domain=0:1,samples=50,smooth,lava, ultra thick] {0.5*x};
\addplot[domain=1:2,samples=50,smooth,lava, ultra thick] {0};
\addplot[domain=2:3,samples=50,smooth,lava, ultra thick] {-0.5*(x-2)};
\addplot[domain=3:4,samples=50,smooth,lava, ultra thick] {0};
\addplot[domain=4:5,samples=50,smooth,lava, ultra thick] {0.5*(x-4)};
\addplot[domain=5:6,samples=50,smooth,lava, ultra thick] {0};
\addplot[domain=6:7,samples=50,smooth,lava, ultra thick] {-0.5*(x-6)};
\addplot[domain=7:8,samples=50,smooth,lava, ultra thick] {0};
\draw [ ultra thick, lava,dashed] (1,0) -- (1,0.5);
\draw [ ultra thick, lava,dashed] (3,0) -- (3,-0.5);
\draw [ ultra thick, lava,dashed] (5,0) -- (5,0.5);
\draw [ ultra thick, lava,dashed] (7,0) -- (7,-0.5);
\end{axis}
\end{tikzpicture}
\end{document}