5

I re-worded this to confirm to the idea of S.O. (thanks Michael0x2a)

I have been trying to find the x value associated with the maximum of a histogram plotted in matplotlib.pyplot. At first I had trouble even finding out how to access the data of the histogram just using the code

import matplotlib.pyplot as plt

# Dealing with sub figures...
fig = plt.figure()
ax = fig.add_subplot(111)
ax.hist(<your data>, bins=<num of bins>, normed=True, fc='k', alpha=0.3)

plt.show()

Then after doing some reading online (and around these forums) I found that you can 'extract' the histogram data like so:

n, bins, patches = ax.hist(<your data>, bins=<num of bins>, normed=True, fc='k', alpha=0.3)

Basically I need to know how to find the value of bins that the maximum n corresponds to!

Cheers!

FriskyGrub
  • 979
  • 3
  • 14
  • 25

3 Answers3

7

You can also do this with a numpy function.

elem = np.argmax(n)

Which will be much faster than a python loop (doc).

If you do want to write this as a loop, I would write it as such

nmax = np.max(n)
arg_max = None
for j, _n in enumerate(n):
    if _n == nmax:
        arg_max = j
        break
print b[arg_max]
tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • I fail to see the difference between this loop and my own. It does the exact same thing the exact same way, except the `range(0,len(n))`, and the `else:` you do some pre-definitions., it is really just a matter of style. .... But I did not know about `np.argmax(n)` thanks for that. Still no reason to neg a question people! – FriskyGrub Sep 17 '13 at 04:29
  • @FriskyGrub 1) Style is surprising important in python. 2) I don't know if numpy caches the maximum value. If it does not, then your method (which gets the max every time through) is O(N**2) – tacaswell Sep 17 '13 at 12:12
  • and if these solved your problem, please accept (big gray check box on the left) – tacaswell Sep 17 '13 at 12:13
  • Yeah cheers :) ... It is generally a matter of.. oh I see, you found the max outside the loop... tricky :D ... still difference between a micro and nano second for small arrays I would guess :) Thanks though – FriskyGrub Sep 17 '13 at 12:19
  • Yes, but it would be quite a shock if you end up using this on a large array. – tacaswell Sep 17 '13 at 12:22
7
import matplotlib.pyplot as plt
import numpy as np
import pylab as P

mu, sigma = 200, 25
x = mu + sigma*P.randn(10000)

n, b, patches = plt.hist(x, 50, normed=1, histtype='stepfilled')

bin_max = np.where(n == n.max())

print 'maxbin', b[bin_max][0]
Robert
  • 5,278
  • 43
  • 65
  • 115
Guest
  • 71
  • 1
  • 1
0

This can be achieved with a simple 'find-n-match' sort of approach

import matplotlib.pyplot as plt

# Yur sub-figure stuff
fig = plt.figure()
ax = fig.add_subplot(111)
n,b,p=ax.hist(<your data>, bins=<num of bins>)

# Finding your point
for y in range(0,len(n)):
    elem = n[y]
    if elem == n.max():
     break
else:   # ideally this should never be tripped
    y = none
print b[y] 

so b is the 'x values' list, b[y] is the 'x value' corresponding to n.max() Hope that helps!

FriskyGrub
  • 979
  • 3
  • 14
  • 25
  • Gee thanks for the neg! Any comment as to why please? ... If it isn't efficient enough, that is no reason to neg someone. It does the job and this is NOT a duplicate question. – FriskyGrub Sep 17 '13 at 04:24
  • 1
    Welcome to StackOverflow! While I wasn't the one to downvote, I suspect someone did because your question really isn't a question at all. While self-answering is encouraged, you'd get a better reception if you worded both your question and answer to be independent of each other. Specifically, the question should be phrased as an actual question, showing an attempt as research and all the other prereqs of a good question, and not as "look at this cool thing I found out!". Remember, StackOverflow is a Q&A site, and every post is expected to conform to that format. Hope that helps! – Michael0x2a Sep 17 '13 at 06:29