如何更改np.histogram中的y轴数据?

问题描述 投票:0回答:1

我有一个从数据创建直方图的代码。数据包含“点”区域。现在直方图绘制了每个类别中的点数,但我想计算每平方米的点数(使用

calculate_dots_per_m2
函数完成),然后绘制直方图,其中 y 数据为dots/m^2 而不是原始的dots/mm^2。这是我的代码:

import numpy as np
from matplotlib import pyplot as plt

color_dict = {
    "RED": (0, 0, 255),
    "GREEN": (0, 225, 0),
    "BLUE": (255, 0, 0),
    "ORANGE": (0, 165, 255),
    "MAGENTA": (255, 0, 255)
    }

color_texts = list(color_dict.keys())
colors = list(color_dict.values())
all_areas = [0.01, 0.1, 1, 10, 100]

def calculate_dots_per_m2(num_sizes):
    scale_factor = 25
    num_of_dots_per_m2 = []
    for i, num_of_defects in enumerate(num_sizes):
        num_of_dots_per_m2.append(num_of_defects*scale_factor)
    total_dots_per_m2 = sum(num_of_dots_per_m2)
    return num_of_dots_per_m2, total_dots_per_m2

def make_histogram():
    dots = [[] for i in range(len(all_areas))]
    all_dot_areas = [0.05, 1.4, 1.5, 1.9, 3.7, 11.5, 29.1, 99.0, 111.1, 4123.1]

    for dot_area in all_dot_areas:
        for i, a in enumerate(all_areas[:-1]):
            if all_areas[i] < dot_area < all_areas[i + 1]:
                dots[i].append(dot_area)
        if dot_area > all_areas[-1]:
            dots[-1].append(dot_area)

    num_sizes = [len(subls) for subls in dots]
    num_of_dots_per_m2, total_dots_per_m2 = calculate_dots_per_m2(num_sizes)

    bins = all_areas + [1e9]
    hist, bin_edges = np.histogram(all_dot_areas, bins)
    fig, ax = plt.subplots()
    fig.set_size_inches(16, 9)
    ylabel = "Number of dots per mm^2"
    plt.ylabel(ylabel)

    ticklabels = []
    for i, a in enumerate(all_areas[:-1]):
        size_x_label = "Dots sized {}-{} mm^2: {}".format(all_areas[i], all_areas[i + 1], num_sizes[i])
        ticklabels.append(size_x_label)
    lastlabel = "Dots with size over {} mm^2: {}".format(all_areas[-1], num_sizes[-1])
    ticklabels.append(lastlabel)

    colours = [c.lower() for c in color_texts]
    ax.bar(range(len(hist)), hist, width=0.75, align="center", tick_label=ticklabels, color=colours, ec="black")
    plt.show()

make_histogram()

我想更改代码,使 x 数据相同(x 是每个类别中的点数),但 y 数据缩放为 m^2(每 m^2 的点数)。因此,在这个简单的示例中,我预计所有柱线都比现在高 25 倍。我尝试将这个

hist, bin_edges = np.histogram(all_dot_areas, bins)
更改为这个
hist, bin_edges = np.histogram(num_of_dots_per_m2, bins)
,但没有帮助。我不明白我应该在这里做什么。

numpy histogram
1个回答
0
投票

可以使用

weights
hist
参数:

hist, bin_edges = np.histogram(all_dot_areas, bins, 
                               weights=np.full_like(all_dot_areas, 25))
© www.soinside.com 2019 - 2024. All rights reserved.