調整單刻度的樣式(例如位置)

調整單刻度的樣式(例如位置)

考慮這個 MWE:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{pgfplots.groupplots}

\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[
        group style={
            group name=my plots,
            group size=1 by 3,
            xlabels at=edge bottom,
            xticklabels at=edge bottom,
            vertical sep=5pt,
        },
        height=3cm,
        ymin=1,
        ymax=3,
        ]

    \nextgroupplot[,
        ytick={1,2,3},
        % yticklabel style={yshift=1mm}, % apply this only to tick at 1
        ]
    \addplot[color=red,mark=x] coordinates {
        (2,1)
        (3,2)
        (4,3)
        };

    \nextgroupplot[,
        ytick={1,2,3},
        % yticklabel style={yshift=-1mm}, % apply this only to tick at 3
        ]
    \addplot[color=red,mark=x] coordinates {
        (2,1)
        (3,2)
        (4,3)
        };
    \end{groupplot}
\end{tikzpicture}
\end{document}

輸出:

在此輸入影像描述

問題陳述:

我想透過將這些標籤分別垂直移動一點來防止兩個刻度“1”和“3”重疊。我的方法是適應yticklabel style.不幸的是,我找不到只適應單一刻度的樣式的方法。

手動解決方案對很多人來說都很好——我不需要自動刻度標籤重疊預防

答案1

也許你可以明確設定yticklabels並使用\raisebox\smash

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{pgfplots.groupplots}
\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
    group style={
      group name=my plots,
      group size=1 by 3,
      xlabels at=edge bottom,
      xticklabels at=edge bottom,
      vertical sep=5pt,
    },
    height=3cm,
    ymin=1,
    ymax=3,
    ]
  \nextgroupplot[,
    ytick={1,2,3},
    yticklabels={\smash{1},2,\smash{\raisebox{-\height}{3}}},
    ]
  \addplot[color=red,mark=x] coordinates {
    (2,1)
    (3,2)
    (4,3)
    };
  \nextgroupplot[,
    ytick={1,2,3},
    yticklabels={\smash{1},2,\smash{\raisebox{-\height}{3}}},
    ]
  \addplot[color=red,mark=x] coordinates {
    (2,1)
    (3,2)
    (4,3)
    };
  \nextgroupplot[,
    ytick={1,2,3},
    yticklabels={\smash{1},2,\smash{\raisebox{-\height}{3}}},
    ]
  \addplot[color=red,mark=x] coordinates {
    (2,1)
    (3,2)
    (4,3)
    };
  \end{groupplot}
\end{tikzpicture}
\end{document}

在此輸入影像描述

或者你可以使用\yticklabel

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{pgfplots.groupplots}

\newcommand\myyticklabel[2]{%
  \ifnum\ticknum=#1%
    \smash{\axisdefaultticklabel}%
  \else%
    \ifnum\ticknum=#2%
      \smash{\raisebox{-\height}{\axisdefaultticklabel}}%
    \else%
      \axisdefaultticklabel%
    \fi%
  \fi%
}


\begin{document}
\begin{tikzpicture}
  \begin{groupplot}[
    group style={
      group name=my plots,
      group size=1 by 3,
      xlabels at=edge bottom,
      xticklabels at=edge bottom,
      vertical sep=5pt,
    },
    height=3cm,
    ymin=1,
    ymax=3,
    ]
  \nextgroupplot[,
    ytick={1,2,3},
    yticklabel={\myyticklabel{0}{2}}
    ]
  \addplot[color=red,mark=x] coordinates {
    (2,1)
    (3,2)
    (4,3)
    };
  \nextgroupplot[,
    ytick={1,2,3},
    yticklabel={\myyticklabel{0}{2}}
    ]
  \addplot[color=red,mark=x] coordinates {
    (2,1)
    (3,2)
    (4,3)
    };
  \nextgroupplot[,
    ytick={1,2,3},
    yticklabel={\myyticklabel{0}{2}}
    ]
  \addplot[color=red,mark=x] coordinates {
    (2,1)
    (3,2)
    (4,3)
    };
  \end{groupplot}
\end{tikzpicture}
\end{document}

注意第一個ticknum是0,第三個是2。

答案2

我剛剛找到了答案,謝謝這個回答作者:Jake,使用extra y ticksextra y tick style

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{pgfplots.groupplots}


\begin{document}
\begin{tikzpicture}
    \begin{groupplot}[
        group style={
            group name=my plots,
            group size=1 by 3,
            xlabels at=edge bottom,
            xticklabels at=edge bottom,
            vertical sep=5pt,
        },
        height=3cm,
        ymin=1,
        ymax=3,
        ]

    \nextgroupplot[,
        ytick={2,3},
        extra y ticks={1},
        extra y tick style={
            yticklabel style={yshift=0.5ex}
            },
        ]
    \addplot[color=red,mark=x] coordinates {
        (2,1)
        (3,2)
        (4,3)
        };

    \nextgroupplot[,
        ytick={1,2},
        extra y ticks={3},
        extra y tick style={
            yticklabel style={yshift=-0.5ex}
            },      
        ]
    \addplot[color=red,mark=x] coordinates {
        (2,1)
        (3,2)
        (4,3)
        };
    \end{groupplot}
\end{tikzpicture}
\end{document}

在此輸入影像描述

相關內容