Simple way for using multiple Numpy Arrays as an input for one Seaborn boxplot
Asked Answered
M

1

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.

Military answered 5/3, 2018 at 19:23 Comment(0)
L
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

Lecialecithin answered 5/3, 2018 at 21:28 Comment(5)
parfait merci !Buckman
@Lecialecithin 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?Planchette
@Employee copied.Planchette
@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)]).Inexpensive
Note that the arrays don't need to be the same sizeInexpensive

© 2022 - 2024 — McMap. All rights reserved.