![調整單刻度的樣式(例如位置)](https://rvso.com/image/309861/%E8%AA%BF%E6%95%B4%E5%96%AE%E5%88%BB%E5%BA%A6%E7%9A%84%E6%A8%A3%E5%BC%8F%EF%BC%88%E4%BE%8B%E5%A6%82%E4%BD%8D%E7%BD%AE%EF%BC%89.png)
考慮這個 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 ticks
和extra 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}