是否可以在\addplot
命令中使用命名节点作为坐标?
pgfplots
我正在尝试绘制一条线,该线穿过通过(而不是使用boxplot prepared
)计算的多个箱线图的平均值。我天真地认为命名平均节点并\addplot
在下面的 MWE 中将它们用作单独命令中的输入坐标,但结果却引发了错误。不幸的是,仅使用绘制线条似乎无法正确连接线段,我必须为已经为 定义的tikz
线条样式定义单独的线条样式。tikz
pgfplots
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}
\pgfplotstableread{
A B C
5 2 1
2 3 6
1 4 5
2 5 4
1 6 6
7 5 2
3 6 4
}{\datatable}
\begin{tikzpicture}
\begin{axis}
[boxplot/draw direction=y,
boxplot/average=auto, boxplot/every average/.style={mark=},
ytick={1,3,...,9}, xtick={1,2,3}, xticklabels={A, B, C}
]
\addplot+[boxplot] table[y=A]\datatable node (A) at (boxplot box cs: \boxplotvalue{average}, 0.5) {};
\addplot+[boxplot] table[y=B]\datatable node (B) at (boxplot box cs: \boxplotvalue{average}, 0.5) {};
\addplot+[boxplot] table[y=C]\datatable node (C) at (boxplot box cs: \boxplotvalue{average}, 0.5) {};
\draw [ultra thick, red] (A) -- (B) -- (C);
%\addplot[ultra thick, red] coordinates { (A) (B) (C) };
% Runaway argument? A) (B) (C) \pgfplots@EOI \pgfplotsscanlinelengthcleanup \pgfplots@coord@stream@ end \ETC.
% ! File ended while scanning use of \pgfplots@foreach@plot@coord@NEXT@.
\end{axis}
\end{tikzpicture}
\end{document}
答案1
使用coordinate (..) at (...);
而不是node (..) at (...) {};
,则 可以按预期工作。另一种方法是使用 时\draw
更改为,则线条将在节点中心之间绘制,而不是像默认的那样从边到边绘制。\draw (A.center) -- (B.center) -- (C.center);
node
axis
如果您已经在选项或中定义了样式,\pgfplotsset
那么您可以\draw
使用来应用它/pgfplots/stylename
。
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}
\pgfplotstableread{
A B C
5 2 1
2 3 6
1 4 5
2 5 4
1 6 6
7 5 2
3 6 4
}{\datatable}
\begin{tikzpicture}
\begin{axis}
[boxplot/draw direction=y,
boxplot/average=auto, boxplot/every average/.style={mark=},
ytick={1,3,...,9}, xtick={1,2,3}, xticklabels={A, B, C},
someline/.style={ultra thick,red}
]
\addplot+[boxplot] table[y=A]\datatable coordinate (A) at (boxplot box cs: \boxplotvalue{average}, 0.5);
\addplot+[boxplot] table[y=B]\datatable coordinate (B) at (boxplot box cs: \boxplotvalue{average}, 0.5);
\addplot+[boxplot] table[y=C]\datatable coordinate (C) at (boxplot box cs: \boxplotvalue{average}, 0.5);
\draw [/pgfplots/someline] (A) -- (B) -- (C);
\end{axis}
\end{tikzpicture}
\end{document}