How to Draw a PGFPlots Boxplot from One Data Row
At a glance
- 1. Identify the observations
Treat the values in the chosen row as one sample; exclude its label or identifier.
- 2. Put values in a column
PGFPlots expects raw boxplot observations in a column, with one observation per table row.
- 3. Plot the column
Load the statistics library and pass the observation column to an addplot command with the boxplot option.
- 4. Check quartiles
Confirm the quartile convention is suitable for your use, since software can use different conventions.
A PGFPlots boxplot is built from a set of observations. In the usual table format, each observation occupies a separate row in one column. If your data set stores each sample on a single line, first extract that line’s values into a column, then plot the column.
The example below shows a complete boxplot from seven observations. After it, a short Python script demonstrates how to extract a selected row from a CSV file.
How PGFPlots reads boxplot data
Load PGFPlots’ statistics library to enable the boxplot plot handler. For raw observations, the general form is:
\addplot+[boxplot] table[y index=0] { ... };
Here, y index=0 selects the first column (column indexes start at zero). Each table row supplies one observation. If your values are spread across several columns on one line, PGFPlots will not treat that line as one sample automatically: reformat it as a column first.
A complete example
This example plots the values 2, 3, 4, 5, 6, 7, 8 as one boxplot. Save it as a .tex file and compile with a LaTeX engine such as pdfLaTeX.
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ylabel={Value},
xtick={1},
xticklabels={Selected row},
]
\addplot+[boxplot] table[y index=0] {
value
2
3
4
5
6
7
8
};
\end{axis}
\end{tikzpicture}
\end{document}
The value line is the column heading; the following lines are the observations. PGFPlots calculates the boxplot statistics from those observations. The exact quartile values can depend on the software’s convention, especially for small samples.
Extract a row from a CSV file
Suppose your file has a header and rows like this:
group,a,b,c,d,e
first,2,3,4,5,6
second,4,5,7,8,10
To plot the second row, write its numeric fields to a one-column file. This Python script selects the second data row (row indexes start at zero, so 1 means the second row after the header) and skips the first field, which is the group label:
import csv
row_index = 1
with open("data.csv", newline="", encoding="utf-8") as source:
rows = list(csv.reader(source))
selected = rows[row_index + 1] # +1 skips the header
with open("selected-row.dat", "w", encoding="utf-8") as output:
output.write("value\n")
for value in selected[1:]: # skip the group label
output.write(value + "\n")
Then replace the inline table in the LaTeX example with:
\addplot+[boxplot] table[y index=0] {selected-row.dat};
If your file has no header, adjust the row offset. If it has an ID or label column, skip that field when extracting the observations. The values passed to the boxplot should be numeric.
If the data is already in columns
If each observation is already on its own line in one column, you do not need to extract or transpose anything. Point the table command at that column. For example, y index=2 selects the third column. If each column is a separate group, make one \addplot+[boxplot] command per group, selecting the corresponding column each time.
Common mistakes
- Passing a row-shaped table directly: PGFPlots reads observations down a column. Rearrange the chosen row into a column first.
- Including a label as a value: Text labels and IDs are not sample observations. Leave them out of the numeric column.
- Using the wrong column index:
y index=0means the first column, not the second. - Expecting identical quartiles across tools: Quartile definitions can differ. If exact quartiles are specified, calculate them using the required convention and use PGFPlots’ prepared-statistics boxplot options instead of raw observations.
References
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.