Luca
Luca

Reputation: 10996

plotting box plot with seaborn with multidimensional data

I am trying to create a box plot with seaboard as follows:

I have some synthetic data where I have 24 different categories which are generated as:

import numpy as np
x = np.arange(10, 130, step=5)

Now, for each of these categories I generate 5 random observations as follows:

y = np.zeros(shape=(len(y), 5)) # Each row contains 5 observations for a category

Now, what I want to do is do a box plot with seaboard where I plot these 5 values along the y-axes (highlighting the confidence interval) and on the x-axes, I would like each of these categories. So, I do:

import seaborn as sis
fig = sns.boxplot(x=x, y=y)
fig.plt.show()

However, this comes with the exception that the data must be 1-dimensional. I am not sure how to structure my data so that I can plot it.

Upvotes: 0

Views: 3109

Answers (1)

lanery
lanery

Reputation: 5364

The problem, as you point out, is the shape of your input data. Without trying to make too many assumptions as to what you are trying to do, I think you are looking for something like

x = np.arange(10, 130, step=5)
y = 4 * np.random.randn(x.size, 5) + 3

x_for_boxplot = np.repeat(x, 5)
y_for_boxplot = y.flatten()

ax = sns.boxplot(x_for_boxplot, y_for_boxplot)

where x_for_boxplot and y_for_boxplot have been restructured so that they are 1D arrays of the same size which is what sns.boxplot is looking for. I also changed y so that it is made up of random normal values rather than zeros.

enter image description here

Upvotes: 1

Related Questions