首页 > 解决方案 > 直方图的最后一个 bin 缺失

问题描述

首先,这个答案对我不起作用,但问题本质上是一样的。我的数据x是 [-2:18] 范围内的列表,标记为 [A:U]。最后一个 bin(17 或 T)实际上是在累积 17-T 和 18-U 的值的数量,显示 bin 18-U 为空。

我的代码看起来像这样(美学已被省略,x是从 .csv 读取的):

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=figsize)

Labels = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
       'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U']
bins = len(Labels)
ax.hist(x, bins=bins, density=False, histtype='step', color='grey', linewidth=2) 
ax.set_xticklabels(Labels)

plt.show()

结果是这样的:

在此处输入图像描述

尝试现有的解决方案,bins = len(Labels) + 1没有任何区别。

标签: matplotlibhistogram

解决方案


推荐阅读