单个 ytick 标签不动

单个 ytick 标签不动

代码:

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{float}
%\pgfplotsset{compat=1.16}
\usepackage{tikz}
\usepackage{siunitx}
\usetikzlibrary{shapes, arrows.meta, automata, positioning, matrix, calc}
\usepackage[margin=1in]{geometry} 
\begin{document} 
    \begin{figure}[H]\centering
        \begin{tikzpicture}
        \begin{axis}[
        width = 5 in,
        height = 2.5 in,
        scale only axis,
        xtick={1.5, 3, 4.5, 6},
        xticklabels={$\frac{T}{2}$, $T$, $\frac{3T}{2}$, $2T$},
        ytick={-0.5, 0.5},
        yticklabels={\vspace*{1cm}$-0.5$,  $0.5$},
        %extra x ticks = {0},
        %xmin=-0.5,xmax=7.5,
        %           extra y ticks = {0},
        xmin=-1,xmax=7.5,
        ymin=-1,ymax=1,
        axis lines=center,
        axis line style=->, xlabel = {$t$}, ylabel={$V_\textrm{in}$ ($\SI{}{\volt}$)},
        %every axis y label/.style={at={(0,1)},anchor=south},
        %every axis x label/.style={at={(1,2/3)},anchor=west},
        xticklabel style = {xshift=-0.35cm},
        every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
        every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,},
        domain=-pi:pi,samples=101]
        \addplot [blue,thick]coordinates
        {(-1,-0.5) (0,-0.5) (0,0.5) (1.5,0.5) (1.5,-0.5) (3,-0.5) (3,0.5) (4.5, 0.5) (4.5, -0.5) (6, -0.5) (6, 0.5) (7.5, 0.5)};
        \node at(axis cs:0.75,0.25) {Region 1};
        \node at(axis cs:2.25,-0.25) {Region 2};
        \end{axis}
        \end{tikzpicture} 
        \caption{Square wave input.}
        \label{SqIn}
    \end{figure}
\end{document}

输出: 在此处输入图片描述

我试图将yticklabel -0.5向下移动,但并没有。我甚至尝试了解决方案此链接,但效果不太好。

可能是什么问题呢?

答案1

您可以使 y 刻度标签的样式取决于\ticknum

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{float}
\pgfplotsset{compat=1.16}
\usepackage{siunitx}
\usepackage[margin=1in]{geometry} 
\begin{document} 
    \begin{figure}[H]\centering
        \begin{tikzpicture}
        \begin{axis}[
        width = 5 in,
        height = 2.5 in,
        scale only axis,
        xtick={1.5, 3, 4.5, 6},
        xticklabels={$\frac{T}{2}$, $T$, $\frac{3T}{2}$, $2T$},
        ytick={-0.5, 0.5},
        yticklabels={$-0.5$,  $0.5$},
        yticklabel style={yshift=1em-\ticknum*1em},
        %extra x ticks = {0},
        %xmin=-0.5,xmax=7.5,
        %           extra y ticks = {0},
        xmin=-1,xmax=7.5,
        ymin=-1,ymax=1,
        axis lines=center,
        axis line style=->, xlabel = {$t$}, ylabel={$V_\textrm{in}$ ($\SI{}{\volt}$)},
        %every axis y label/.style={at={(0,1)},anchor=south},
        %every axis x label/.style={at={(1,2/3)},anchor=west},
        xticklabel style = {xshift=-0.35cm},
        every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
        every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,},
        domain=-pi:pi,samples=101]
        \addplot [blue,thick]coordinates
        {(-1,-0.5) (0,-0.5) (0,0.5) (1.5,0.5) (1.5,-0.5) (3,-0.5) (3,0.5) (4.5, 0.5) (4.5, -0.5) (6, -0.5) (6, 0.5) (7.5, 0.5)};
        \node at(axis cs:0.75,0.25) {Region 1};
        \node at(axis cs:2.25,-0.25) {Region 2};
        \end{axis}
        \end{tikzpicture} 
        \caption{Square wave input.}
        \label{SqIn}
    \end{figure}
\end{document}

在此处输入图片描述

extra y ticks或者也可以这样玩

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{float}
\pgfplotsset{compat=1.16}
\usepackage{siunitx}
\usepackage[margin=1in]{geometry} 
\begin{document} 
    \begin{figure}[H]\centering
        \begin{tikzpicture}
        \begin{axis}[
        width = 5 in,
        height = 2.5 in,
        scale only axis,
        xtick={1.5, 3, 4.5, 6},
        xticklabels={$\frac{T}{2}$, $T$, $\frac{3T}{2}$, $2T$},
        ytick={0.5},
        yticklabels={$0.5$},
        extra y ticks={-0.5},
        every extra y tick/.style={yshift=1em},
        %extra x ticks = {0},
        %xmin=-0.5,xmax=7.5,
        %           extra y ticks = {0},
        xmin=-1,xmax=7.5,
        ymin=-1,ymax=1,
        axis lines=center,
        axis line style=->, xlabel = {$t$}, ylabel={$V_\textrm{in}$ ($\SI{}{\volt}$)},
        %every axis y label/.style={at={(0,1)},anchor=south},
        %every axis x label/.style={at={(1,2/3)},anchor=west},
        xticklabel style = {xshift=-0.35cm},
        every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
        every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,},
        domain=-pi:pi,samples=101]
        \addplot [blue,thick]coordinates
        {(-1,-0.5) (0,-0.5) (0,0.5) (1.5,0.5) (1.5,-0.5) (3,-0.5) (3,0.5) (4.5, 0.5) (4.5, -0.5) (6, -0.5) (6, 0.5) (7.5, 0.5)};
        \node at(axis cs:0.75,0.25) {Region 1};
        \node at(axis cs:2.25,-0.25) {Region 2};
        \end{axis}
        \end{tikzpicture} 
        \caption{Square wave input.}
        \label{SqIn}
    \end{figure}
\end{document}

得出的结果相同。\ticknum是刻度的编号/索引。第一个有\ticknum=0,第二个有\ticknum=1,依此类推。因此,可以引入交替对齐。

\documentclass[12pt]{article}
\usepackage{pgfplots}
\usepackage{float}
\pgfplotsset{compat=1.16}
\usepackage{siunitx}
\usepackage[margin=1in]{geometry} 
\begin{document} 
    \begin{figure}[H]\centering
        \begin{tikzpicture}
        \begin{axis}[
        width = 5 in,
        height = 2.5 in,
        scale only axis,
        xtick={1.5, 3, 4.5, 6},
        xticklabels={$\frac{T}{2}$, $T$, $\frac{3T}{2}$, $2T$},
        ytick={-0.5, 0.5},
        yticklabels={$-0.5$,  $0.5$},
        yticklabel style={anchor=180+\ticknum*180},
        %extra x ticks = {0},
        %xmin=-0.5,xmax=7.5,
        %           extra y ticks = {0},
        xmin=-1,xmax=7.5,
        ymin=-1,ymax=1,
        axis lines=center,
        axis line style=->, xlabel = {$t$}, ylabel={$V_\textrm{in}$ ($\SI{}{\volt}$)},
        %every axis y label/.style={at={(0,1)},anchor=south},
        %every axis x label/.style={at={(1,2/3)},anchor=west},
        xticklabel style = {xshift=-0.35cm},
        every axis x label/.style={at={(ticklabel* cs:1.05)}, anchor=west,},
        every axis y label/.style={at={(ticklabel* cs:1.05)}, anchor=south,},
        domain=-pi:pi,samples=101]
        \addplot [blue,thick]coordinates
        {(-1,-0.5) (0,-0.5) (0,0.5) (1.5,0.5) (1.5,-0.5) (3,-0.5) (3,0.5) (4.5, 0.5) (4.5, -0.5) (6, -0.5) (6, 0.5) (7.5, 0.5)};
        \node at(axis cs:0.75,0.25) {Region 1};
        \node at(axis cs:2.25,-0.25) {Region 2};
        \end{axis}
        \end{tikzpicture} 
        \caption{Square wave input.}
        \label{SqIn}
    \end{figure}
\end{document}

在此处输入图片描述

答案2

您可以使用

 yticklabels={\raisebox{-1cm}{$-0.5$},  $0.5$}

在此处输入图片描述

相关内容