3

I've got a 2D numpy array with dimensions (500, 10) that I'd like to plot as a Seaborn violinplot or boxplot where there is a box for each of the 10 columns. What is the cleanest way to pass this to Seaborn without doing a bunch of tedious manipulation to get it into a Pandas Dataframe first? I'm confident that I can do the transformation, but I'm afraid I'm likely not understanding the best and most concise way to do so.

For example, I could do something thing like

all_arrays = []
cols = the_array.shape[1]
for col in range(0, cols):
     all_arrays.append(the_array[:, col])
sns.boxplot(data=all_arrays)

But is there a better way to split the original array into a list of arrays, or perhaps there's a better way to pass this into seaborn? Thanks.

1 Answer 1

4

Your solution is correct, boxplot() expects a list of vectors, so you have to somehow transform your matrix into that.

You can simplify the way you write your code however: sns.boxplot(data=[d for d in the_array.T])

full code:

# create a dummy matrix 500x10
the_array = np.zeros(shape=(500,10))
for i in range(10):
    the_array[:,i] = np.random.normal(loc=i, size=(500,))

sns.boxplot(data=[d for d in the_array.T])

enter image description here

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

Comments

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.