Lok Jun
Lok Jun

Reputation: 1333

Matplotlib - Box plot with number of occurrence of data?

Assuming I have an array

data = np.array([(1,2,3),
                 (2,3,4),
                 (1,2,5)])

Usually, I can simply put

plot = plt.boxplot(data)

and have the data created for me, easy as day.

However, what should I do when what I have is the number of occurrences of the data?

In this case, I have the following arrays

occur = np.array([2,1,0,0,0],[0,2,1,0,0],[0,0,1,1,1])
#                 1,2,3,4,5   1,2,3,4,5   1,2,3,4,5
#first columns have "1" appear 2 times, "2" appear 1 time, "3" appear 0 times...
#second columns have "1" appear 0  times, "2" appear 2 times, "3" appear 1 time ...

I tried plt.boxplot(weights = data) but obviously it didn't work

Upvotes: 0

Views: 395

Answers (1)

rLevv
rLevv

Reputation: 508

Not sure if you can manipulate the boxplot arguments to make this faster but reconstructing from a bincount (which is what you have) can be pretty straightforward.

occur = np.column_stack(np.repeat(np.arange(x.size)+1, x) for x in occur)

outputs this array to occur:

array([[1, 2, 3],
       [1, 2, 4],
       [2, 3, 5]])

Then,

plt.boxplot(occur)

gives the boxplot you want.

Upvotes: 1

Related Questions