Pgfplots: valori discreti anziché connessi
Ho una tabella con dati sperimentali. La tabella ha due colonne separate da un punto e virgola. La prima colonna è il valore per l'asse x e la seconda è il valore per l'asse y.
Se uso il \addplot table {}comando generale , pgfplotsdisegna un grafico in cui collega i punti insieme. Poiché i punti dati in questione sono valori discreti, non dovrebbe esserci una connessione tra di loro, ma piuttosto ogni punto dovrebbe essere tracciato in modo indipendente.
Qual è l'approccio corretto per questo utilizzo pgfplots?
Output generato dal \addplot table {}comando
\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}
Uscita prevista
Questo output è stato generato da un piccolo "hack". Per avere l'asse corretto, ho disegnato la trama come prima ma ho cambiato il colore in red!0modo che fosse trasparente. Quindi, per ogni voce della tabella, ho disegnato manualmente le linee con il \drawcomando. Va da sé che questo non è ottimale, ma almeno illustra l'output desiderato.
\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}
Risposte
Penso che tu stia cercando 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}
Come mostrato nella risposta anonima , ycombpuò fare il lavoro. Un altro modo è usare ybar. Questo traccia ogni punto dati come una barra verticale (per le barre orizzontali, useresti xbar). La larghezza della barra può essere personalizzata utilizzando bar width, come in:
\begin{axis}[..., ybar, bar width=0.4pt]
\addplot table[draw=none] {table.csv};
\end{axis}
Come hai scoperto tu stesso, draw=noneconsente di ottenere la larghezza specificata precisa per le barre, altrimenti vengono disegnate leggermente più grandi a causa drawdell'operazione.
Esempio completo:
\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}
Se vuoi bar widthche sia uguale al PGF corrente line widthin tikzpicture, puoi usare 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}