python histo 改变 bins 大小_在Python中显示具有非常不均匀的bin宽度的直方图
這是直方圖
為了生成這個圖,我做了:
bins = np.array([0.03, 0.3, 2, 100])
plt.hist(m, bins = bins, weights=np.zeros_like(m) + 1. / m.size)
但是,正如您所注意到的,我想繪制每個數據點的相對頻率的直方圖,只有3個不同大小的區間:
bin1 = 0.03 – > 0.3
bin2 = 0.3 – > 2
bin3 = 2 – > 100
由于最后一個bin的大小相對于其他bin非常大,因此直方圖看起來很難看.如何修復直方圖?我想改變箱子的寬度,但我不想改變每個箱子的范圍.
[email?protected],這不再是直方圖,但你可以用plt.bar和np.histogram來做你想要的.然后,您只需將xticklabels設置為描述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,您會發現該欄現在需要一個kwarg tick_label,這可以使這個繪圖更容易(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)])
總結
以上是生活随笔為你收集整理的python histo 改变 bins 大小_在Python中显示具有非常不均匀的bin宽度的直方图的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 钉钉机器人发送图片 python_pyt
- 下一篇: python高阶函数filter_Pyt