我正在使用pgfplots
绘制一个简单的 2D 函数。我想在图的右侧显示所有 ytick 标签。到目前为止,我使用 还没有成功yticklabel pos=right
,我没有收到错误,但标签只是在左侧呈现。这可能吗?
这是我的代码(有一些缩短):
\documentclass[a4paper,fontsize=11pt]{scrreprt}
\usepackage{pgfplots}
\begin{document}
\begin{figure}[htb]
\begin{tikzpicture}
\begin{axis}[
ticks=none,
ymajorticks=true,
xmajorticks=true,
axis x line=bottom,
axis y line=left,
enlargelimits = true,
ymin=0, xmin=0,
xlabel={Zeit},
ylabel={kum. fakturierbarer Wert},
title style={at={(0.5,-0.2)},anchor=north,align=center},
title={{\textsf{\textbf{\textsc{Mit Teilmengen}}}}},
xtick={0,3,6},
xticklabels={Startdatum,heute,Enddatum},
xlabel style={at={(current axis.right of origin)},anchor=north east,xshift=5mm},
ylabel style={at={(current axis.left of origin)},xshift=15mm},
x tick label style={anchor=north},
extra x ticks={0,3,6},
extra x tick labels={},
extra x tick style={grid=major},
ytick={6},
yticklabels={Planwert},
yticklabel pos=right,
extra y ticks={6},
extra y tick labels={},
extra y tick style={grid=major}]
\addplot [mark=*,mark size=0.7] coordinates {
(0, 0) (1, 0) (1, 1) (2, 1) (2, 2) (3, 2)
(3, 3) (4, 3) (4, 4) (5, 4) (5, 5) (6, 5)
(6, 6) (7, 6) (8, 6)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
这是我所拥有的图片:
下面是我想要的图片:
答案1
这里有一些解决方法。
添加clip=false
然后在情节末尾添加一个节点。
\documentclass{article}
\usepackage{pgfplots}
\def\lblfont{\itshape}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
clip=false,
width=.97\textwidth,
ticks=none,
ymajorticks=true,
xmajorticks=true,
axis x line=bottom,
axis y line=left,
enlargelimits = true,
ymin=0, xmin=0,
xlabel={Zeit},
ylabel={kum. fakturierbarer Wert},
title style={at={(0.5,-0.2)},anchor=north,align=center},
title={{\textsf{\textbf{\textsc{Mit Teilmengen}}}}},
xtick={0,3,6},
xticklabels={\lblfont{Startdatum},\lblfont{heute},\lblfont{Enddatum}},
xlabel style={at={(current axis.right of origin)},anchor=north,yshift=-10mm},
ylabel style={at={(current axis.left of origin)},xshift=15mm},
x tick label style={anchor=north},
extra x ticks={0,3,6},
extra x tick labels={},
extra x tick style={grid=major},
ytick=\empty,
extra y ticks={6},
extra y tick labels={},
extra y tick style={grid=major}
]
\addplot [mark=*,mark size=0.7] coordinates {
(0, 0) (1, 0) (1, 1) (2, 1) (2, 2) (3, 2)
(3, 3) (4, 3) (4, 4) (5, 4) (5, 5) (6, 5)
(6, 6) (7, 6) (8, 6)}node[pos=1.11]{\lblfont{Planwert}};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
事实上,我通过反复试验找到了另一种解决方案。
我将 包裹tikzpicture
在 中minipage
,并使用了这个技巧将文本相对于 的右端定位为 ,minipage
如下所示y tick label style={at={(current axis.east)},anchor=west,xshift=\textwidth}
。这可能有点不太容易被黑客攻击,而且对我来说似乎效果更好,所以我认为分享会很好,尽管它可能不是最好的解决方案。
以下是完整代码(可编译状态):
\documentclass[a4paper,fontsize=11pt]{scrreprt}
\usepackage{pgfplots}
\newcommand{\lblfont}[1]{\fontsize{8.5}{4}\selectfont \emph{#1}}
\begin{document}
\begin{figure}[htb]
\begin{minipage}[t]{.5\textwidth} %%% The function plot occupies half of the page
\begin{tikzpicture}
\begin{axis}[
ticks=none,
ymajorticks=true,
xmajorticks=true,
axis x line=bottom,
axis y line=left,
enlargelimits = true,
ymin=0, xmin=0,
xlabel={Zeit},
ylabel={kum. fakturierbarer Wert},
title style={at={(0.5,-0.2)},anchor=north,align=center},
title={{\textsf{\textbf{\textsc{Mit Teilmengen}}}}},
xtick={0,3,6},
xticklabels={\lblfont{Startdatum},\lblfont{heute},\lblfont{Enddatum}},
xlabel style={at={(current axis.right of origin)},anchor=north east},
x tick label style={anchor=north},
y tick label style={at={(current axis.east)},anchor=west,xshift=\textwidth}, %%% <---- Little trick
extra x ticks={0,3,6},
extra x tick labels={},
extra x tick style={grid=major},
ytick={6},
yticklabels={\lblfont{Planwert}},
extra y ticks={6},
extra y tick labels={},
extra y tick style={grid=major}]
\addplot [mark=*,mark size=0.7] coordinates {
(0, 0) (1, 0) (1, 1) (2, 1) (2, 2) (3, 2)
(3, 3) (4, 3) (4, 4) (5, 4) (5, 5) (6, 5)
(6, 6) (7, 6) (8, 6)};
\end{axis}
\end{tikzpicture}
\end{minipage}
\end{figure}
\end{document}
视觉效果如下:
答案3
无论出于什么原因,yticklabel pos=upper ???
这是来自 pgfplots_1.18.1 文档:
“第二个变化相当简单:我们只在第二个轴上添加了选项 yticklabel pos=upper。这会将所有刻度标签移到右侧,而不会改变任何其他内容。”