numpys histogram2d中的参数顺序

时间:2016-11-22 11:49:31

标签: python numpy matplotlib

我对于如何在histogram2d中定向轴感到有些困惑。 doc给出了以下示例:

H, xedges, yedges = np.histogram2d(y, x, bins=(xedges, yedges))

请注意,y和x在输入参数中翻转,但不在输出中翻转。 根据doc:

,可以通过这样的imshow来查看此操作的结果
im = plt.imshow(H, interpolation='nearest', origin='low',extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]])

但是,在这种情况下,轴似乎会翻转,如下例所示:

import numpy as np 
import matplotlib.pyplot as plt
xdata = np.ones(10)*1
ydata = np.ones(10)*2
H,xedges,yedges = np.histogram2d(ydata,xdata)
plt.imshow(H,origin='low', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]],interpolation='none')
plt.show()

enter image description here

如您所见,x和y轴被切换 - 生成的点全部位于(1,2)。当然,可以将extent =中的条目翻转为更改为轴,但这没有帮助,如下所示:

R = [[0.5,1.5],[2,3]]
H,xedges,yedges = np.histogram2d(ydata,xdata,range=R)
plt.imshow(H,origin='low', extent=[yedges[0], yedges[-1], xedges[0], xedges[-1]],interpolation='none')
plt.show()

enter image description here

唯一似乎做我想做的就是这个;

H,yedges,xedges = np.histogram2d(ydata,xdata)
plt.imshow(H,origin='low', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]],interpolation='none')
plt.show()
R = [[2,3],[0.5,1.5]]
H,yedges,xedges = np.histogram2d(ydata,xdata,range=R)
plt.imshow(H,origin='low', extent=[xedges[0], xedges[-1], yedges[0], yedges[-1]],interpolation='none')
plt.show()

enter image description here

enter image description here

也就是说,我必须翻转所有输入参数和输出参数,当然还有范围参数。这是使用这个例程的正确方法,还是我错过了一些重要的事情?

0 个答案:

没有答案