我正在尝试为量子物理学中的优化参数制作条形图。为了提供更多信息,图表应在条形上方显示这些参数的值。到目前为止,我做到了
\documentclass{minimal}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[title={$E_0$ über $n$}, ymax=2.5, yticklabels=]
\addplot [blue!90!white, fill=blue!50!white, ybar] coordinates {
( 1, 1.96556 )
( 2, 1.86105 )
( 3, 1.93185 )
( 4, 2.02568 )
( 5, 2.12077 )
( 6, 2.21219 )
};
\pgfplotsforeachungrouped \x/\y in {1/1.96556, 2/1.86105, 3/1.93185, 4/2.02568, 5/2.12077, 6/2.21219} {
\edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y}] at (axis cs:\x,\y) {};}\temp
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
此代码输出以下图表
现在我尝试在杆和销钉之间增加一点空间,但我失败了。我尝试++ (axis cs:0,0.1)
\edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y}] at (axis cs:\x,\y) ++ (axis cs:0,0.1) {};}\temp
和++ (axis direction cs:0,0.1)
\edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y}] at (axis cs:\x,\y) ++ (axis direction cs:0,0.1) {};}\temp
两种变体都会出现此错误:
! Package tikz Error: A node must have a (possibly empty) label text.
我也试过pos=0.1
\edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y},pos=0.1] at (axis cs:\x,\y) {};}\temp
编译成功,但输出如下
提前谢谢您,maeru。
答案1
我将尽可能坚持pgfplots
使用节点样式。您可以将引脚选项传递给nodes near coords
以及许多其他选项(颜色、线条样式等),如下所示:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[title={$E_0$ über $n$}, ymax=2.5, yticklabels=,
nodes near coords={\pgfmathprintnumber[precision=4]\pgfplotspointmeta},
nodes near coords align={
shift={(0,1.5cm)},
rotate=90,
pin={[pin distance = 0.75cm,
pin edge={thick,double=yellow}]180:},
text=red
}
]
\addplot [blue!90!white, fill=blue!50!white, ybar] coordinates {
( 1, 1.96556 )
( 2, 1.86105 )
( 3, 1.93185 )
( 4, 2.02568 )
( 5, 2.12077 )
( 6, 2.21219 )
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
制作帽子的一种方法是使用 tikz 的calc
库来调整放置大头针的坐标:
($(axis cs:\x,\y)+(0.0cm,0.1cm)$)
代码:
\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[title={$E_0$ über $n$}, ymax=2.5, yticklabels=]
\addplot [blue!90!white, fill=blue!50!white, ybar] coordinates {
( 1, 1.96556 )
( 2, 1.86105 )
( 3, 1.93185 )
( 4, 2.02568 )
( 5, 2.12077 )
( 6, 2.21219 )
};
\pgfplotsforeachungrouped \x/\y in {1/1.96556, 2/1.86105, 3/1.93185, 4/2.02568, 5/2.12077, 6/2.21219} {
\edef\temp{\noexpand\node[coordinate, pin={[rotate=90]0:\y}] at ($(axis cs:\x,\y)+(0.0cm,0.1cm)$) {};}\temp
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案3
我的解决方案
这个问题有很多很好的答案,但我选择了阻力最小的路径,只是添加了shift={(0,0.1cm)}
\edef\temp{\noexpand\node[coordinate,shift={(0,0.1cm)},pin = {[rotate=90]0:\y}] at (axis cs:\x,\y) { };}