2025, Oct 27 23:00
How to draw a single box plot for an entire pandas DataFrame matrix by stacking all values
Learn how to plot a single box for a 2D pandas DataFrame: stack values into one column to combine all matrix entries into one distribution. With code.
One box plot for a whole matrix in pandas: how to combine everything into a single distribution
It’s a common surprise: you pass a 2D array into a pandas DataFrame and call boxplot, expecting a single summary of the entire dataset, but end up with multiple boxes. If your goal is one box plot that represents all values in the matrix together, there’s a straightforward way to get it.
Problem setup
The following snippet builds a 4×4 matrix, converts it to a DataFrame, and draws a box plot. The result is four separate boxes.
import numpy as npa
import pandas as pds
vals = npa.random.random(size=(4, 4))
frame = pds.DataFrame(vals)
frame.boxplot()Why multiple boxes appear
You are creating 2D data with shape (4, 4). By default, a DataFrame boxplot produces one box per column, so a 4-column table yields four different boxes. If the goal is a single plot summarizing all 16 values (4 × 4), the data needs to be treated as one consolidated set rather than four separate columns.
The fix
To generate a single box plot for the entire matrix, first reshape the DataFrame so that all values become one column. After that, calling boxplot will produce a single box representing all 16 samples.
import numpy as npa
import pandas as pds
vals = npa.random.random(size=(4, 4))
frame = pds.DataFrame(vals)
frame.stack().to_frame().boxplot()This stacks the 2D structure into a single column and then plots it, resulting in one box for the whole dataset.
Why this matters
Sometimes you want to compare columns; sometimes you want a single summary of everything you have. When you need one distribution for a matrix, treating all entries as one pool avoids misleading per-column splits and focuses on the aggregate behavior.
Takeaways
If a DataFrame boxplot shows multiple boxes and you want just one, make the data one dimensional before plotting. For a matrix, stacking into a single column is an effective way to consolidate values, after which boxplot will render a single box that summarizes the entire dataset.
The article is based on a question from StackOverflow by stefaniecg and an answer by Panda Kim.