TeX - LaTeX Asked on January 6, 2021
How can I enhance the appearance of the xtick labels (shift them to the top of the plotted lines.
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}
By default, the height of a graph is axisdefaultheight
. I move the labels by half this height: xticklabel style={yshift=.5*axisdefaultheight}
As noted by @Torbjørn T. in his commentary, this solution becomes more general by using the pgf key pgfkeysvalueof{/pgfplots/height}
instead of 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}
Answered by AndréC on January 6, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP