请详细解释Python中的2D直方图 [英] Please explain in detail 2D Histogram in Python

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

问题描述

我试图了解2D直方图的值.

I am trying to understand what are the values of a 2D histogram.

我有2个numpy数组,它们的长度分别为X和Y(每一个中的浮点数).

I have 2 numpy arrays of the same length X and Y (float numbers in each one).

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

For example the first 10 values of X: [ 88, 193, 60, 98, 78, 100, 75, 76, 130]

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

and 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)) 

我得到一个二维直方图.

I get a 2D histogram.

但这是什么意思?

一维直方图简单地向我显示了我拥有的每个物品的数量.

1D histogram simply shows me how much of each item I have.

请解释一下2D中的含义.

Please explain me what does it mean in 2D.

提前谢谢!

推荐答案

假设您有一个1D数组,将其值的位置绘制在x轴上,它们是如此密集,以至于您无法分辨出空间分布,您可以使用一维直方图来显示沿x轴的框数分布情况.问题解决了.

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.

然后您有两个1D数组,即(x,y)轴上的2D点列表.您在x-y平面上绘制它们的位置,它们又是如此密集并且彼此重叠.您希望通过平面中的框数更好地查看分布,因此您尝试使用2D图.问题解决了.

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.

这是一个例子

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')

给予

# 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')

给予

这篇关于请详细解释Python中的2D直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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