python histo 改变 bins 大小_在Python中显示具有非常不均匀的bin宽度的直方图
這是直方圖
為了生成這個(gè)圖,我做了:
bins = np.array([0.03, 0.3, 2, 100])
plt.hist(m, bins = bins, weights=np.zeros_like(m) + 1. / m.size)
但是,正如您所注意到的,我想繪制每個(gè)數(shù)據(jù)點(diǎn)的相對(duì)頻率的直方圖,只有3個(gè)不同大小的區(qū)間:
bin1 = 0.03 – > 0.3
bin2 = 0.3 – > 2
bin3 = 2 – > 100
由于最后一個(gè)bin的大小相對(duì)于其他bin非常大,因此直方圖看起來(lái)很難看.如何修復(fù)直方圖?我想改變箱子的寬度,但我不想改變每個(gè)箱子的范圍.
[email?protected],這不再是直方圖,但你可以用plt.bar和np.histogram來(lái)做你想要的.然后,您只需將xticklabels設(shè)置為描述bin邊緣的字符串.例如:
import numpy as np
import matplotlib.pyplot as plt
bins = [0.03,0.3,2,100] # your bins
data = [0.04,0.07,0.1,0.2,0.2,0.8,1,1.5,4,5,7,8,43,45,54,56,99] # random data
hist, bin_edges = np.histogram(data,bins) # make the histogram
fig,ax = plt.subplots()
# Plot the histogram heights against integers on the x axis
ax.bar(range(len(hist)),hist,width=1)
# Set the ticks to the middle of the bars
ax.set_xticks([0.5+i for i,j in enumerate(hist)])
# Set the xticklabels to a string that tells us what the bin edges were
ax.set_xticklabels(['{} - {}'.format(bins[i],bins[i+1]) for i,j in enumerate(hist)])
plt.show()
編輯
如果您更新到matplotlib v1.5.0,您會(huì)發(fā)現(xiàn)該欄現(xiàn)在需要一個(gè)kwarg tick_label,這可以使這個(gè)繪圖更容易(see here):
hist, bin_edges = np.histogram(data,bins)
ax.bar(range(len(hist)),hist,width=1,align='center',tick_label=
['{} - {}'.format(bins[i],bins[i+1]) for i,j in enumerate(hist)])
總結(jié)
以上是生活随笔為你收集整理的python histo 改变 bins 大小_在Python中显示具有非常不均匀的bin宽度的直方图的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 钉钉机器人发送图片 python_pyt
- 下一篇: strcmp可以比较数组么_C语言数组越