如何获取直方图中条形右上角的坐标?到目前为止,我只是尝试使线条合适……
\begin{document}
\usepackage{tikz}
\begin{tikzpicture}
\begin{axis}[ybar, bar width=20pt,ytick=\empty,xtick=data,symbolic x coords={$c_{i1}$,$c_{i2}$,$c_{i3}$}, axis x line=bottom, axis y line=left,ymin=0, x=1.5cm, enlarge x limits=0.4]
\addplot[fill=white] coordinates{
($c_{i1}$,5)
($c_{i2}$,7)
($c_{i3}$,15)
};
\end{axis}
\draw[dashed,thick] (1.6,1.9) -- (5.1,1.9);
\draw[dashed,thick] (4.55,5.7) -- (5.1,5.7);
\end{tikzpicture}
\end{document}
答案1
这是一个可行的方法。使用symbolic coordiante
正 10pt(条宽的一半)找到角并为其分配标签。通过相对坐标从那里画一条线。
代码
\documentclass[tikz,border=5]{standalone}
\usepackage{tikz,pgfplots}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar, bar width=20pt,ytick=\empty,xtick=data,symbolic x coords={$c_{i1}$,$c_{i2}$,$c_{i3}$},
axis x line=bottom, axis y line=left,ymin=0, x=1.5cm, enlarge x limits=0.4]
\addplot[fill=white] coordinates{
($c_{i1}$,5)
($c_{i2}$,7)
($c_{i3}$,15)
};
\path[] (axis cs:{$c_{i1}$},5) ++(10pt,0)coordinate(a){};
\path[] (axis cs:{$c_{i3}$},15) ++(10pt,0)coordinate(b){};
\end{axis}
\draw[dashed,thick] (a) -- ++(4,0);
\draw[dashed,thick] (b) -- ++(3,0);
\end{tikzpicture}
\end{document}