如何向 tikz 中的条形图添加单个数据点?

如何向 tikz 中的条形图添加单个数据点?

我对 LaTeX 还比较陌生,我正在尝试使用 Tikz 创建条形图。条形图是平均值,我想将各个值作为点添加到每个条形中,如下图所示: https://i.stack.imgur.com/O96hD.png

我尝试添加另一个 tikzpicture,但没有按预期工作 :D 我知道我也可以创建节点,但我还没弄清楚如何知道我必须将它们放置在什么坐标上。提前感谢您的帮助!

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}

\begin{tikzpicture}[scale=9]

\begin{axis}[axis x line* = center, axis y line* = left, ymin=0,
enlarge x limits=1, 
symbolic x coords={Control,PINK-KO},xtick={Control,PINK-KO
}, ybar, legend style={font=\tiny}
]
\addplot [fill=gray!99, draw=black!70,] coordinates
{(Control,19.5274) (PINK-KO,13.589)};
\addplot [fill=gray!75, draw=black!70] coordinates
{(Control,11.7355) (PINK-KO,14.1069)};
\addplot [fill=gray!25, draw=black!70] coordinates
{(Control,5.73582) (PINK-KO,2.799175)};
\addplot [fill=white, draw=black!70]coordinates
{(Control,14.6213) (PINK-KO,3.58424)};
\legend{Standard protocol, Extended SHH signaling, SHH signaling after 24h, SHH signaling after 36h}
\end{axis}


\begin{axis}[axis x line* = center, axis y line* = left, ymin=0,
enlarge x limits=1, yticklabels={,,}, xticklabels={,,},
symbolic x coords={Control,PINK-KO}, xticklabel=\empty, xtick={Control,PINK-KO
}, legend style={font=\tiny}, ytick style={draw=none}, xtick style={draw=none}
]
\addplot [scatter, only marks, mark size=1] coordinates
{(Control,5.5) (PINK-KO,1.5)};
\addplot [scatter, only marks, mark size=1] coordinates
{(Control,3.5) (PINK-KO,2.5)};
\addplot [scatter, only marks, mark size=1] coordinates
{(Control,2.5) (PINK-KO,3.5)};
\addplot [scatter, only marks, mark size=1] coordinates
{(Control,1.5) (PINK-KO,4.5)};

\end{axis}

\end{tikzpicture}


\end{document}

答案1

我猜你正在寻找这样的东西:

在此处输入图片描述

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
    \begin{tikzpicture}[scale=9]
\begin{axis}[
    axis lines* = left,
    ybar,
    ymin=0,
    enlarge x limits=1,
    enlarge y limits={upper,value=0.2},
    symbolic x coords={Control,PINK-KO},
    xtick={Control,PINK-KO},
    legend style={font=\tiny},
        mark size=1, draw=black!70
            ]
\addplot [fill=gray!75] coordinates {(Control,19.5274) (PINK-KO,13.589)};
\addplot [fill=gray!50] coordinates {(Control,11.7355) (PINK-KO,14.1069)};
\addplot [fill=gray!25] coordinates {(Control,5.73582) (PINK-KO,2.799175)};
\addplot [fill=white]   coordinates {(Control,14.6213) (PINK-KO,3.58424)};
\legend{Standard protocol, Extended SHH signaling,
        SHH signaling after 24h, SHH signaling after 36h}
%%
\addplot +[only marks, xshift=-6.4mm] coordinates {(Control,5.5) (PINK-KO,2.5)};
\addplot +[only marks, xshift=-2.2mm] coordinates {(Control,3.5) (PINK-KO,1.5)};
\addplot +[only marks, xshift= 2.2mm] coordinates {(Control,2.5) (PINK-KO,3.5)};
\addplot +[only marks, xshift= 6.4mm] coordinates {(Control,1.5) (PINK-KO,4.5)};
\end{axis}
    \end{tikzpicture}
\end{document}

附註: 如果您希望在 y 上设置分散的标记,则需要重新排列\addplots 并按如下方式绘制它们:

在此处输入图片描述

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}

\begin{document}
    \begin{tikzpicture}[scale=9]
\begin{axis}[
    axis lines* = left,
    ybar,
    ymin=0,
    enlarge x limits=1,
    enlarge y limits={upper,value=0.2},
    symbolic x coords={Control,PINK-KO},
    xtick={Control,PINK-KO}, 
    legend style={font=\tiny},
        mark size=1, draw=black!70
            ]
\addplot [fill=gray!75] coordinates {(Control,19.5274) (PINK-KO,13.589)};
\addplot [fill=gray!50] coordinates {(Control,11.7355) (PINK-KO,14.1069)};
\addplot [fill=gray!25] coordinates {(Control,5.73582) (PINK-KO,2.799175)};
\addplot [fill=white]   coordinates {(Control,14.6213) (PINK-KO,3.58424)};
\legend{Standard protocol, Extended SHH signaling, 
        SHH signaling after 24h, SHH signaling after 36h}
%%
\addplot +[only marks, xshift=-6.4mm] coordinates 
    {(Control,5.5) (Control,5.5) (Control,15) (Control,18) (Control,22) (Control,24)};
\addplot +[only marks, xshift=-2.2mm] coordinates 
    {(Control,8.5) (Control,9.5) (Control,10) (Control,6) (Control,12) (Control,14)};
\addplot +[only marks, xshift= 2.2mm] coordinates  
    {(Control,4.5) (Control,5) (Control,5.5) (Control,6) (Control,7) (Control,8)};
\addplot +[only marks, xshift= 6.4mm] coordinates 
    {(Control,11.5) (Control,13.5) (Control,15) (Control,16) (Control,17) (Control,18)};
%
\addplot +[only marks, xshift=-6.4mm] coordinates 
    {(PINK-KO,9.5) (PINK-KO,11) (PINK-KO,12) (PINK-KO,13) (PINK-KO,15) (PINK-KO,17)};
\addplot +[only marks, xshift=-2.2mm] coordinates 
    {(PINK-KO,10) (PINK-KO,12) (PINK-KO,13) (PINK-KO,14) (PINK-KO,14.5) (PINK-KO,18)};
\addplot +[only marks, xshift= 2.2mm] coordinates 
{(PINK-KO,1.5) (PINK-KO,2) (PINK-KO,2.5) (PINK-KO,3) (PINK-KO,3.5) (PINK-KO,5)};
\addplot +[only marks, xshift= 6.4mm] coordinates 
{(PINK-KO,1.5) (PINK-KO,2.5) (PINK-KO,4) (PINK-KO,4.5) (PINK-KO,5) (PINK-KO,5.5)};
\end{axis}
    \end{tikzpicture}
\end{document}

在这种情况下,用标记坐标定义表似乎更方便。

相关内容