16

我试图了解二维直方图的值是什么。

我有 2 个长度相同的 X 和 Y 的 numpy 数组(每个数组中的浮点数)。

例如 X 的前 10 个值:[ 88, 193, 60, 98, 78, 100, 75, 76, 130]

和 Y: [ 18. , 9. , 36.1, 18.5, 34.3, 32.9, 32.2, 22. , 15. ]

当我使用:

import matplotlib.pyplot as plt

plt.hist2d(X,Y, bins=(10,20)) 

我得到一个二维直方图。

但是这是什么意思?

一维直方图简单地显示了我拥有的每个项目的数量。

请解释一下它在 2D 中的含义。

提前致谢!

4

1 回答 1

26

Suppose you have a 1D array, you plot the position of its values on the x axis, they are so dense that you can't tell the spatial distribution, you use a 1D histogram to show the distribution by count of boxes along the x axis. Problem solved.

Then you have two 1D arrays, a list of 2D dots in (x, y) axes. You plot their positions on the x-y plane, again they are so dense and overlap with each other. You want to view the distribution better by count of boxes in the plane, so you try a 2D diagram. Problem solved.

Here is an example

import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline

# prepare 2D random dots centered at (0, 0)
n = 100000
x = np.random.randn(n)
y = x + np.random.randn(n)

# plot data
fig1 = plt.figure()
plt.plot(x,y,'.r')
plt.xlabel('x')
plt.ylabel('y')

gives

enter image description here

# plot 2D histogram using pcolor
fig2 = plt.figure()
plt.hist2d(x, y, bins=100)
plt.xlabel('x')
plt.ylabel('y')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Counts')

gives

enter image description here

于 2017-02-16T00:38:45.957 回答