Pgfplots:离散值而非连通值

Pgfplots:离散值而非连通值

我有一张包含实验数据的表格。该表格有两列,两列之间用分号分隔。第一列是 x 轴的值,第二列是 y 轴的值。

如果我使用常规\addplot table {}命令,pgfplots则会绘制一个将点连接在一起的图。由于所涉及的数据点是离散值,因此它们之间不应该存在连接,而应该独立绘制每个点。

这种使用的正确方法是什么pgfplots

\addplot table {}命令生成的输出

默认输出

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat = 1.17}
\usepackage{siunitx}

\begin{filecontents}{table.csv}
    57.7200;403.7083
    81.1200;359.9954
    83.7600;2673.1575
    84.4800;201.8542
    95.7600;359.9954
    104.6400;403.7083
    104.8800;2673.1575
    105.0000;5146.4829
    105.1200;11393.4863
    105.2400;5706.3101
    105.9600;201.8542
    106.9200;4277.3057
    107.2800;359.9954
    107.4000;682.9842
    107.8800;2673.1575
    108.0000;403.7083
    108.3600;2673.1575
    108.6000;201.8542
    109.2000;47.6190
    109.4400;3883.1460
    109.9200;4081.7891
    110.0400;1113.2275
\end{filecontents}


\begin{document}

\begin{tikzpicture}
    
    \begin{axis}[
        xlabel = {\textit{m/z}},
        ylabel = {Int. [\si{cps}]},
    ]
        \addplot+[mark = none, red] table[
            col sep = semicolon
        ] {table.csv};
    \end{axis}

\end{tikzpicture}

\end{document}

预期输出

预期输出

此输出是通过一个小“黑客”生成的。为了获得正确的轴,我像以前一样绘制了图,但将颜色更改为red!0透明。然后,对于每个表条目,我使用命令手动绘制线条\draw。不用说,这不是最佳选择,但至少它说明了所需的输出。

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat = 1.17}
\usepackage{siunitx}

\begin{filecontents}{table.csv}
    57.7200;403.7083
    81.1200;359.9954
    83.7600;2673.1575
    84.4800;201.8542
    95.7600;359.9954
    104.6400;403.7083
    104.8800;2673.1575
    105.0000;5146.4829
    105.1200;11393.4863
    105.2400;5706.3101
    105.9600;201.8542
    106.9200;4277.3057
    107.2800;359.9954
    107.4000;682.9842
    107.8800;2673.1575
    108.0000;403.7083
    108.3600;2673.1575
    108.6000;201.8542
    109.2000;47.6190
    109.4400;3883.1460
    109.9200;4081.7891
    110.0400;1113.2275
\end{filecontents}


\begin{document}

\begin{tikzpicture}
    
    \begin{axis}[
        xlabel = {\textit{m/z}},
        ylabel = {Int. [\si{cps}]},
    ]
        \addplot+[mark = none, red!0] table[
            col sep = semicolon
        ] {table.csv};
        \draw[red] (57.7200,0) -- (57.7200,403.7083);
        \draw[red] (81.1200,0) -- (81.1200,359.9954);
        \draw[red] (83.7600,0) -- (83.7600,2673.1575);
        \draw[red] (84.4800,0) -- (84.4800,201.8542);
        \draw[red] (95.7600,0) -- (95.7600,359.9954);
        \draw[red] (104.6400,0) -- (104.6400,403.7083);
        \draw[red] (104.8800,0) -- (104.8800,2673.1575);
        \draw[red] (105.0000,0) -- (105.0000,5146.4829);
        \draw[red] (105.1200,0) -- (105.1200,11393.4863);
        \draw[red] (105.2400,0) -- (105.2400,5706.3101);
        \draw[red] (105.9600,0) -- (105.9600,201.8542);
        \draw[red] (106.9200,0) -- (106.9200,4277.3057);
        \draw[red] (107.2800,0) -- (107.2800,359.9954);
        \draw[red] (107.4000,0) -- (107.4000,682.9842);
        \draw[red] (107.8800,0) -- (107.8800,2673.1575);
        \draw[red] (108.0000,0) -- (108.0000,403.7083);
        \draw[red] (108.3600,0) -- (108.3600,2673.1575);
        \draw[red] (108.6000,0) -- (108.6000,201.8542);
        \draw[red] (109.2000,0) -- (109.2000,47.6190);
        \draw[red] (109.4400,0) -- (109.4400,3883.1460);
        \draw[red] (109.9200,0) -- (109.9200,4081.7891);
        \draw[red] (110.0400,0) -- (110.0400,1113.2275);
    \end{axis}

\end{tikzpicture}

\end{document}

答案1

我认为您正在寻找ycomb

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat = 1.17}
\usepackage{siunitx}

\begin{filecontents}[overwrite]{table.csv}
    57.7200;403.7083
    81.1200;359.9954
    83.7600;2673.1575
    84.4800;201.8542
    95.7600;359.9954
    104.6400;403.7083
    104.8800;2673.1575
    105.0000;5146.4829
    105.1200;11393.4863
    105.2400;5706.3101
    105.9600;201.8542
    106.9200;4277.3057
    107.2800;359.9954
    107.4000;682.9842
    107.8800;2673.1575
    108.0000;403.7083
    108.3600;2673.1575
    108.6000;201.8542
    109.2000;47.6190
    109.4400;3883.1460
    109.9200;4081.7891
    110.0400;1113.2275
\end{filecontents}


\begin{document}

\begin{tikzpicture}
    
    \begin{axis}[
        xlabel = {\textit{m/z}},
        ylabel = {Int. [\si{cps}]},
    ]
        \addplot+[mark = none, red,ycomb] table[
            col sep = semicolon
        ] {table.csv};
    \end{axis}

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

如图所示匿名' 的回答ycomb可以完成这项工作。另一种方法是使用ybar。这会将每个数据点绘制为垂直条(对于水平条,您可以使用xbar)。可以使用自定义条宽bar width,如下所示:

\begin{axis}[..., ybar, bar width=0.4pt]
    \addplot table[draw=none] {table.csv};
\end{axis}

正如您自己发现的那样,draw=none这允许人们获得条形的精确指定宽度,否则由于操作,它们会被绘制得稍微大一些draw

完整示例:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{siunitx}

\begin{filecontents}{table.csv}
    57.7200;403.7083
    81.1200;359.9954
    83.7600;2673.1575
    84.4800;201.8542
    95.7600;359.9954
    104.6400;403.7083
    104.8800;2673.1575
    105.0000;5146.4829
    105.1200;11393.4863
    105.2400;5706.3101
    105.9600;201.8542
    106.9200;4277.3057
    107.2800;359.9954
    107.4000;682.9842
    107.8800;2673.1575
    108.0000;403.7083
    108.3600;2673.1575
    108.6000;201.8542
    109.2000;47.6190
    109.4400;3883.1460
    109.9200;4081.7891
    110.0400;1113.2275
\end{filecontents}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        xlabel = {\textit{m/z}},
        ylabel = {Int. [\si{cps}]},
        ybar, bar width = 0.4pt,
    ]
        \addplot+[draw=none, fill=red] table[col sep = semicolon] {table.csv};
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

如果希望bar width等于line width中的当前 PGF tikzpicture,则可以使用bar width/.expanded = \the\pgflinewidth

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepackage{siunitx}

\begin{filecontents}{table.csv}
    57.7200;403.7083
    81.1200;359.9954
    83.7600;2673.1575
    84.4800;201.8542
    95.7600;359.9954
    104.6400;403.7083
    104.8800;2673.1575
    105.0000;5146.4829
    105.1200;11393.4863
    105.2400;5706.3101
    105.9600;201.8542
    106.9200;4277.3057
    107.2800;359.9954
    107.4000;682.9842
    107.8800;2673.1575
    108.0000;403.7083
    108.3600;2673.1575
    108.6000;201.8542
    109.2000;47.6190
    109.4400;3883.1460
    109.9200;4081.7891
    110.0400;1113.2275
\end{filecontents}

\begin{document}

\begin{tikzpicture}[line width=2pt]
    \begin{axis}[
        xlabel = {\textit{m/z}},
        ylabel = {Int. [\si{cps}]},
        ybar, bar width/.expanded = \the\pgflinewidth,
    ]
        \addplot+[draw=none, fill=red] table[col sep = semicolon] {table.csv};
    \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容