Python中数组的2D和3D散布直方图 [英] 2D and 3D Scatter Histograms from arrays in Python

查看:107
本文介绍了Python中数组的2D和3D散布直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您有什么主意,我如何将3个数组归为直方图.我的数组看起来像

have you any idea, how I can bin 3 arrays to a histogram. My arrays look like

Temperature = [4,   3,   1,   4,   6,   7,   8,   3,   1]
Radius      = [0,   2,   3,   4,   0,   1,   2,  10,   7]
Density     = [1,  10,   2,  24,   7,  10,  21, 102, 203]

一维图应显示为:

Density

     |           X
10^2-|               X
     |       X
10^1-|   
     |   X
10^0-|
     |___|___|___|___|___   Radius
         0  3.3 6.6  10

二维图应(定性)如下:

And the 2D plot should (qualitative) look like:

Density

     |           2      | |
10^2-|      11249       | |
     |     233          | | Radius
10^1-|    12            | |
     |   1              | |
10^0-|
     |___|___|___|___|___   Temperature
         0   3   5   8

所以我想用python/numpy合并一个或两个字段,然后将它们绘制出来以分析它们的对应关系.

So I want to bin one or two fields with python/numpy and then plot them to analyse their correspondence.

推荐答案

此处具有两个功能:hist2d_bubblehist3d_bubble;可能适合您的目的:

Here it follows two functions: hist2d_bubble and hist3d_bubble; that may fit for your purpose:

import numpy as np
import matplotlib.pyplot as pyplot
from mpl_toolkits.mplot3d import Axes3D


def hist2d_bubble(x_data, y_data, bins=10):
    ax = np.histogram2d(x_data, y_data, bins=bins)
    xs = ax[1]
    ys = ax[2]
    points = []
    for (i, j), v in np.ndenumerate(ax[0]):
        points.append((xs[i], ys[j], v))

    points = np.array(points)
    fig = pyplot.figure()
    sub = pyplot.scatter(points[:, 0],points[:, 1],
                         color='black', marker='o', s=128*points[:, 2])
    sub.axes.set_xticks(xs)
    sub.axes.set_yticks(ys)
    pyplot.ion()
    pyplot.grid()
    pyplot.show()
    return points, sub


def hist3d_bubble(x_data, y_data, z_data, bins=10):
    ax1 = np.histogram2d(x_data, y_data, bins=bins)
    ax2 = np.histogram2d(x_data, z_data, bins=bins)
    ax3 = np.histogram2d(z_data, y_data, bins=bins)
    xs, ys, zs = ax1[1], ax1[2], ax3[1]
    smart = np.zeros((bins, bins, bins),dtype=int)
    for (i1, j1), v1 in np.ndenumerate(ax1[0]):
        if v1 == 0:
            continue
        for k2, v2 in enumerate(ax2[0][i1]):
            v3 = ax3[0][k2][j1]
            if v1 == 0 or v2 == 0 or v3 == 0:
                continue
            num = min(v1, v2, v3)
            smart[i1, j1, k2] += num
            v1 -= num
            v2 -= num
            v3 -= num
    points = []
    for (i, j, k), v in np.ndenumerate(smart):
        points.append((xs[i], ys[j], zs[k], v))
    points = np.array(points)
    fig = pyplot.figure()
    sub = fig.add_subplot(111, projection='3d')
    sub.scatter(points[:, 0], points[:, 1], points[:, 2],
                color='black', marker='o', s=128*points[:, 3])
    sub.axes.set_xticks(xs)
    sub.axes.set_yticks(ys)
    sub.axes.set_zticks(zs)
    pyplot.ion()
    pyplot.grid()
    pyplot.show()
    return points, sub

上面的两个图是使用以下方式创建的:

The two figures above were created using:

temperature = [4,   3,   1,   4,   6,   7,   8,   3,   1]
radius      = [0,   2,   3,   4,   0,   1,   2,  10,   7]
density     = [1,  10,   2,  24,   7,  10,  21, 102, 203]
import matplotlib
matplotlib.rcParams.update({'font.size':14})

points, sub = hist2d_bubble(radius, density, bins=4)
sub.axes.set_xlabel('radius')
sub.axes.set_ylabel('density')

points, sub = hist3d_bubble(temperature, density, radius, bins=4)
sub.axes.set_xlabel('temperature')
sub.axes.set_ylabel('density')
sub.axes.set_zlabel('radius')

相关:

如何将一系列浮点值合并到直方图中Python?

如何可以使用python中内置的numpy或matplotlib正确生成3d直方图?

使用Python的2D直方图

这篇关于Python中数组的2D和3D散布直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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