Label histogram by bins matplotlib [duplicate]

坚强是说给别人听的谎言 提交于 2019-12-11 03:01:45

问题


I have a histogram in which I want to label the x-axis by bins. The histogram is plotted as a log log graph, but the bins are very specific. The graph:

The bins:

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

Is there any way I can do this? I believe it would also require getting rid of the current x-axis labels.


回答1:


I wrote an example code for you. Basically, all you need was 'set_xticks' and '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()


来源:https://stackoverflow.com/questions/45228265/label-histogram-by-bins-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!