0

x我有两个我正在比较的领域。它们都是名义上的,只有一和零。男性和女性,默认和无默认(加上更多类似格式的数据)。如果将其放入散点图中,您只会得到四个点,因为当然所有这些情况都会发生,但问题是每种情况下发生了多少次。如果我可以通过将其翻转过来并查看每个点击中这四个点之一的频率来看到它,那将是惊人的。

例如:

x = [1,0,0,0,...1,1,0,1]
y = [0,1,1,0,...1,0,1,0]

我有代码:

def scatterPlot3dFields():
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(x, y, ???)
    plt.show()

但我不知道在 z 轴上放什么来实现这一点。任何帮助都会很棒。

4

1 回答 1

0

听起来您正在寻找np.histogram2d,这是确定二维数据频率(就出现次数而言)的好方法。您可以尝试以下方法:

import numpy as np

# data of 1s and 0s
n_points = 100
x = np.random.randint(2, size=n_points) # ([1, 0, 1, ... , 0, 0, 1])
y = np.random.randint(2, size=n_points) # ([0, 1, 1, ... , 1, 0, 1])

H, xedges, yedges  = np.histogram2d(x, y, bins=2)
# H = frequency from the four points (0, 0), (0,1), (1,0), and (1,1)
# H = ([21, 26], [32, 21]]) for example (must sum to n_points (100))

听起来您也想可视化频率。为此,您可以使用 3D 条形图 ( bar3d)。查看这个matplotlib 示例代码

于 2016-04-30T09:14:22.353 回答