通过 bins matplotlib 标记直方图

Label histogram by bins matplotlib

我有一个直方图,我想在其中用 bin 标记 x 轴。直方图绘制为对数对数图,但 bin 非常具体。图:

垃圾箱:

bins = [0, 0.035, 0.07, 0.15, 0.5, 1, 3, 10, 40]  

有什么办法可以做到这一点吗?我相信它还需要摆脱当前的 x 轴标签。

我为你写了一个示例代码。基本上,您只需要 'set_xticks' 和 'set_xticklabels'.

import numpy as np
import matplotlib.pyplot as plt

x = [0.01, 0.01, 0.01, 0.04, 0.1, 0.1, 0.4, 0.4, 0.4, 0.4, 0.65, 0.65, 0.65, 2, 7, 7, 7, 7, 7, 7, 7, 7, 18, 18, 18]
my_bins = [0.001, 0.035, 0.07, 0.15, 0.5, 1, 3, 10, 40]

ind = np.array(my_bins[:-1])
width = np.array([my_bins[i+1]-my_bins[i] for i in range(len(my_bins)-1)])

fig, ax = plt.subplots()

ax.hist(x, bins=my_bins)
ax.set_xscale('log')
ax.set_xticks(ind + width/2)
ax.set_xticklabels(('bin1', 'bin2', 'bin3', 'bin4', 'bin5', 'bin6', 'bin7', 'bin8'))

plt.show()