Matplotlib直方图集合箱(collection bin)用于高值

37

我有一个值数组,想要创建一个直方图。我主要关注低端数字,并希望将300以上的每个数字收集到一个箱子中。该箱子应该与所有其他(等宽)箱子具有相同的宽度。我该如何做?

注意:这个问题与这个问题相关:在Matplotlib直方图中定义bin宽度/x轴刻度

这是我到目前为止尝试过的:

import matplotlib.pyplot as plt
import numpy as np

def plot_histogram_01():
    np.random.seed(1)
    values_A = np.random.choice(np.arange(600), size=200, replace=True).tolist()
    values_B = np.random.choice(np.arange(600), size=200, replace=True).tolist()

    bins = [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 600]

    fig, ax = plt.subplots(figsize=(9, 5))
    _, bins, patches = plt.hist([values_A, values_B], normed=1,  # normed is deprecated and will be replaced by density
                                bins=bins,
                                color=['#3782CC', '#AFD5FA'],
                                label=['A', 'B'])

    xlabels = np.array(bins[1:], dtype='|S4')
    xlabels[-1] = '300+'

    N_labels = len(xlabels)
    plt.xlim([0, 600])
    plt.xticks(25 * np.arange(N_labels) + 12.5)
    ax.set_xticklabels(xlabels)

    plt.yticks([])
    plt.title('')
    plt.setp(patches, linewidth=0)
    plt.legend()

    fig.tight_layout()
    plt.savefig('my_plot_01.png')
    plt.close()

这是结果,看起来不太美观:enter image description here

然后我改变了带有xlim的那一行:

plt.xlim([0, 325])

以下是结果: enter image description here

它看起来基本符合我的要求,但最后一组数据现在不可见。我需要使用什么技巧来使这个宽度为25的最后一个组数据可视化?

2个回答

56

Numpy有一个方便的函数来处理这个问题:np.clip。尽管名字听起来可能会让人觉得它会删除值,但实际上它只是将它们限制在您指定的范围内。基本上,它在行内执行 Artem 的“脏技巧”。您可以保留值不变,但在hist调用中,只需使用np.clip调用包装数组即可,像这样:

plt.hist(np.clip(values_A, bins[0], bins[-1]), bins=bins)

这种方法有很多好处:

  1. 它更快,对于大量元素而言尤其如此。Numpy 在 C 级别上执行操作,而对 Python 列表(如 Artem 的列表推导式)进行操作会有很多开销。基本上,如果你有使用 Numpy 的选项,你应该使用。

  2. 你可以在需要的地方正确执行,从而减少代码中出错的可能性。

  3. 你不需要保留数组的第二个副本,这样可以减少内存使用(除了这一行之外),进一步降低出错的可能性。

  4. 使用 bins[0],bins[-1] 而不是硬编码的值再次降低出错的可能性,因为你可以在定义 bins 的位置改变它们;无需记住在调用 clip 或其他任何位置都需要更改它们。

因此,将所有内容整合如 OP 中所示:

import matplotlib.pyplot as plt
import numpy as np

def plot_histogram_01():
    np.random.seed(1)
    values_A = np.random.choice(np.arange(600), size=200, replace=True)
    values_B = np.random.choice(np.arange(600), size=200, replace=True)

    bins = np.arange(0,350,25)

    fig, ax = plt.subplots(figsize=(9, 5))
    _, bins, patches = plt.hist([np.clip(values_A, bins[0], bins[-1]),
                                 np.clip(values_B, bins[0], bins[-1])],
                                # normed=1,  # normed is deprecated; replace with density
                                density=True,
                                bins=bins, color=['#3782CC', '#AFD5FA'], label=['A', 'B'])

    xlabels = bins[1:].astype(str)
    xlabels[-1] += '+'

    N_labels = len(xlabels)
    plt.xlim([0, 325])
    plt.xticks(25 * np.arange(N_labels) + 12.5)
    ax.set_xticklabels(xlabels)

    plt.yticks([])
    plt.title('')
    plt.setp(patches, linewidth=0)
    plt.legend(loc='upper left')

    fig.tight_layout()
plot_histogram_01()

上述代码的结果


1
我建议使用 xlabels = bins.astype(str),然后 xlabels[-1] += '+' - leonard vertighel
1
@leonardvertighel 好主意。我已经加上了。谢谢。 - Mike
1
哦,实际上那不是完全正确的。OP似乎想要标签对应于箱子的末尾(除了最后一个),这就是为什么第一行在这种情况下将是xlabels = [str(b) for b in bins[1:]]。两个版本都是合理的,但我会坚持原来的版本,因为它更符合OP的要求。但是将第二行改为xlabels[-1] += '+'绝对是一个真正的改进。 - Mike
作为楼主,我同意@leonardvertighel的观点,应该是325+,好发现! - physicalattraction

6

抱歉,我不熟悉matplotlib。因此,我为您提供一个简单的方法。 我将所有大于300的值放入一个箱子中,并更改了箱子的大小。

问题的根源在于matplotlib试图将所有箱子都放在图表上。在R中,我会将我的箱子转换为因子变量,以便它们不被视为实数。

import matplotlib.pyplot as plt
import numpy as np

def plot_histogram_01():
    np.random.seed(1)
    values_A = np.random.choice(np.arange(600), size=200, replace=True).tolist()
    values_B = np.random.choice(np.arange(600), size=200, replace=True).tolist()
    values_A_to_plot = [301 if i > 300 else i for i in values_A]
    values_B_to_plot = [301 if i > 300 else i for i in values_B]

    bins = [0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 250, 275, 300, 325]

    fig, ax = plt.subplots(figsize=(9, 5))
    _, bins, patches = plt.hist([values_A_to_plot, values_B_to_plot], normed=1,  # normed is deprecated and will be replaced by density
                                bins=bins,
                                color=['#3782CC', '#AFD5FA'],
                                label=['A', 'B'])

    xlabels = np.array(bins[1:], dtype='|S4')
    xlabels[-1] = '300+'

    N_labels = len(xlabels)

    plt.xticks(25 * np.arange(N_labels) + 12.5)
    ax.set_xticklabels(xlabels)

    plt.yticks([])
    plt.title('')
    plt.setp(patches, linewidth=0)
    plt.legend()

    fig.tight_layout()
    plt.savefig('my_plot_01.png')
    plt.close()

plot_histogram_01()

enter image description here


那是一个不太正规的方法,但它能用!在更好的答案出现之前,我接受这个答案。 - physicalattraction

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接