标签直方图按bins matplotlib [英] Label histogram by bins matplotlib

查看:198
本文介绍了标签直方图按bins matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个直方图,我想在其中用垃圾桶标记x轴.直方图以对数对数图的形式绘制,但是分格非常具体.该图:

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:

垃圾箱:

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

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

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

推荐答案

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

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()

这篇关于标签直方图按bins matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆