Panda50
Panda50

Reputation: 939

How to get K max values from a histogram?

I want to extract let say the 3 max values in a matplotlib histogram.

There are a lot of ways to extract the (unique) max value in a histogram, but I don't find anything about extract the 2-3 or 4 max values in a histogram.

I also want it to be automatic (not specific to the following case).

Here is my data and my code:

from matplotlib.pyplot import *    

Angle=[0.0, 0.0, 0.0, 0.0, 1.5526165117219184, 0.0, 1.559560844536934, 0.0, 1.5554129250143014, 1.5529410816553442, 1.5458015331759765, -0.036680787756651845, 0.0, 0.0, 0.0, 0.0, -0.017855245139552514, -0.03224688243525392, 1.5422326689561365, 0.595918005516301, -0.06731387579270513, -0.011627382956383872, 1.5515679276951895, -0.06413211500143158, 0.0, -0.6123221322275954, 0.0, 0.0, 0.13863973713415806, 0.07677189126977804, -0.021735706841792667, 0.0, -0.6099169030770674, 1.546410917622178, 0.0, 0.0, -0.24111767845146836, 0.5961991412974801, 0.014704822377851432]

figure(1,figsize=(16,10))
plt.hist(Angle, bins=100,label='Angle')
show()

Upvotes: 2

Views: 1995

Answers (1)

JohanC
JohanC

Reputation: 80329

plt.hist outputs the bin heights, the bin boundaries and the rectangular patches. np.argsort can sort the values and use the result to index the other arrays.

The code below imports pyplot as plt because importing it as * can lead to al lot of confusion.

import matplotlib.pyplot as plt
import numpy as np

Angle=[0.0, 0.0, 0.0, 0.0, 1.5526165117219184, 0.0, 1.559560844536934, 0.0, 1.5554129250143014, 1.5529410816553442, 1.5458015331759765, -0.036680787756651845, 0.0, 0.0, 0.0, 0.0, -0.017855245139552514, -0.03224688243525392, 1.5422326689561365, 0.595918005516301, -0.06731387579270513, -0.011627382956383872, 1.5515679276951895, -0.06413211500143158, 0.0, -0.6123221322275954, 0.0, 0.0, 0.13863973713415806, 0.07677189126977804, -0.021735706841792667, 0.0, -0.6099169030770674, 1.546410917622178, 0.0, 0.0, -0.24111767845146836, 0.5961991412974801, 0.014704822377851432]
plt.figure(1,figsize=(10, 6))
values, bins, patches = plt.hist(Angle, bins=30)

order = np.argsort(values)[::-1]
print("4 highest bins:", values[order][:4])
print("  their ranges:", [ (bins[i], bins[i+1])   for i in order[:4]])

for i in order[:4]:
    patches[i].set_color('fuchsia')

plt.show()

Output:

4 highest bins: [21.  8.  3.  2.]
  their ranges: [(-0.03315333842372081, 0.03924276080176348), (1.4871647453114498, 1.559560844536934), (-0.1055494376492051, -0.03315333842372081), (0.5460154553801537, 0.6184115546056381)]

resulting plot

Another example highlighting the 3 highest bins:

Angle = np.random.normal(np.tile(np.random.uniform(1, 100, 20 ), 100), 5 )
values, bins, patches = plt.hist(Angle, bins=100)

second example

Upvotes: 2

Related Questions