11

Most of the examples I found use Pandas DataFrame in order to have multiple boxes in a single box plot. I would like to know if there is a simpler, more straight forward way by directly using numpy arrays as the input.

For example, let's take five numpy arrays with each one of them having 20 entries. I would like to plot those five arrays as individual blocks next to each. The block should illustrate the variance of the array entries.

The end result should look something like the second picture on Seaborn's page.

1 Answer 1

22

Simply pass a list of numpy arrays into seaborn's boxplot as it mentions from your very link, the data argument can consist of:

data : DataFrame, array, or list of arrays, optional

import numpy as np
import seaborn as sns

np.random.seed(111)

all_arr = [np.random.uniform(size=20),
           np.random.uniform(size=20),
           np.random.uniform(size=20),
           np.random.uniform(size=20),
           np.random.uniform(size=20)]

sns.boxplot(data=all_arr)

Numpy Array BoxPlot Output

Sign up to request clarification or add additional context in comments.

5 Comments

parfait merci !
@Parfait how do I level the axis to say array1, array2, array3, array4, array5 instead of 0,1,2,3,4 or add a legend to the plot?
@Employee copied.
@arilwan, you can use: ax = sns.boxplot(data=[np.random.uniform(size=20) for i in range(5)]); ax.set_xticklabels([f'array{i}' for i in range(5)]).
Note that the arrays don't need to be the same size

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.