我重新措辞,以确认SO的想法(感谢Michael0x2a)
我一直试图找到与绘制的直方图的最大值相关联的x值matplotlib.pyplot.起初我甚至无法使用代码找到如何访问直方图的数据
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()
Run Code Online (Sandbox Code Playgroud)
然后在网上做了一些阅读(并围绕这些论坛),我发现你可以"提取"直方图数据,如下所示:
n, bins, patches = ax.hist(<your data>, bins=<num of bins>, normed=True, fc='k', alpha=0.3)
Run Code Online (Sandbox Code Playgroud)
基本上我需要知道如何找到bins最大值n对应的值!
干杯!
您也可以使用numpy功能执行此操作.
elem = np.argmax(n)
Run Code Online (Sandbox Code Playgroud)
这将比python循环(doc)快得多.
如果你不希望它写成一个循环,我会写这样
nmax = np.max(n)
arg_max = None
for j, _n in enumerate(n):
if _n == nmax:
arg_max = j
break
print b[arg_max]
Run Code Online (Sandbox Code Playgroud)
小智 5
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]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15206 次 |
| 最近记录: |