scipy 如何用数值计算一组质量的质量图和密度图?

n53p2ov0  于 2022-11-10  发布在  其他
关注(0)|答案(1)|浏览(138)

大家好。我想知道是否有任何方法可以提取质量分布散点图的质量图和质量密度图。
开发成批分配的代码:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from scipy.ndimage.filters import gaussian_filter
from numpy.random import rand

# Finds nran number of random points in two dimensions

def randomizer(nran):
    arr = rand(nran, 2)
    return arr

# Calculates a sort of 'density' plot. Using this from a previous StackOverflow Question: https://stackoverflow.com/questions/2369492/generate-a-heatmap-in-matplotlib-using-a-scatter-data-set

def myplot(x, y, s, bins = 1000):
    plot, xedges, yedges = np.histogram2d(x, y, bins = bins)
    plot = gaussian_filter(plot, sigma = s)
    extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
    return plot.T, extent

试举一例:

arr = randomizer(1000)
plot, extent = myplot(arr[:, 0], arr[:, 1], 20)
fig, ax = plt.subplots(1, 2, figsize = (15, 5))

ax[0].scatter(arr[:, 0], arr[:, 1])
ax[0].set_aspect('equal')
ax[0].set_xlabel('x')
ax[0].set_ylabel('y')
ax[0].set_title('Scatter Plot')

img = ax[1].imshow(plot)
ax[1].set_title('Density Plot?')
ax[1].set_aspect('equal')
ax[1].set_xlabel('x')
ax[1].set_ylabel('y')
plt.colorbar(img)

这会产生一个散点图,我认为这是一个密度图(如有错误,请更正)。现在,假设每个点的质量为50千克。“密度图”是否代表总质量分布图(如果这有意义?),因为颜色条的最大值远小于50。然后,使用该值,我怎样才能计算出这个质量分布的质量密度呢?2如果有人能帮我的话,我将非常感激。3谢谢。
编辑:添加了我从哪里得到热图功能的网站。

ugmeyewa

ugmeyewa1#

好的,我想我找到解决办法了。我想上传这个已经很久了。下面是:


# Importing packages

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm
    from numpy.random import random
    from scipy.stats import binned_statistic_2d

    # Finds nran number of random points in two dimensions
    def randomizer(nran):
        arr_x = []
        arr_y = []
        for i in range(nran):
            arr_x += [10 * random()] # Since random() only produces floats in (0, 1), I multiply by 10 (for illustrative purposes)
            arr_y += [10 *random()] # Since random() only produces floats in (0, 1), I multiply by 10 (for illustrative purposes)
        return arr_x, arr_y

    # Computing weight array
    def weights_array(weight, length):
        weights = np.array([weight] * length)
        return weights

    # Computes a weighted histogram and divides it by the total grid area to get the density 
    def histogramizer(x_array, y_array, weights, num_pixels, Dimension):
        Range = [0, Dimension] # Assumes the weights are distributed in a square area
        grid, _, _, _ = binned_statistic_2d(x_array, y_array, weights, 'sum', bins=num_pixels, range=[Range,Range])
        area = int(np.max(x_array)) * int(np.max(y_array))
        density = grid/area
        return density

然后,实际执行此操作时,会发现:

arr_x, arr_y = randomizer(1000000)
weights = []
for i in range(len(arr_x)):
    weights += [50]
density = histogramizer(arr_x, arr_y, weights, [400,400], np.max(arr_x))
fig, ax = plt.subplots(figsize = (15, 5))
plt.imshow(density, extent = [0, int(np.max(arr_x)), 0, int(np.max(arr_x))]);
plt.colorbar(label = '$kg m^-2$');

我得到的结果如下(我知道通常不建议添加照片,但我想添加它是为了显示代码的输出):

相关问题