Matplotlib 2D直方图似乎已转置 [英] Matplotlib 2D histogram seems transposed

查看:107
本文介绍了Matplotlib 2D直方图似乎已转置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在pyplot中绘制二维直方图:

#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt

MIN, MAX, num = .001, 5000, 500
minn=1
maxx=1000
zbins = 10 ** np.linspace(np.log10(MIN), np.log10(MAX), num)
x=np.linspace(100,600,50000)
y=np.linspace(0,500,50000)

fig1 = plt.figure(1)
counts1,xedges1,edges1,d=plt.hist2d(x,y,bins=zbins)
mesh1 = plt.pcolormesh(zbins, zbins, counts1)
plt.xlim([minn, maxx])
plt.ylim([minn, maxx])
plt.gca().set_xscale("log")
plt.gca().set_yscale("log")
plt.colorbar()

plt.show()

为我可怕的变量命名道歉!

无论如何,当我绘制此图时,直方图似乎切换了x和y轴.我检查了matplotlib 2d hist文档,并确定以正确的顺序排列了x和y参数,但是我一生都无法弄清楚哪里出了问题.任何帮助将不胜感激!

解决方案

混乱的原因是返回的counts数组与您认为的不一样.

plt.hist2d内部使用 numpy.histogram2d 计算二维直方图.文档状态为返回值:

H:ndarray,shape(nx,ny) 样本x和y的二维直方图. x中的值沿第一维直方图,y中的值沿第二维直方图.
xedges:ndarray,shape(nx,) 垃圾箱沿第一维边缘.
yedges:ndarray,shape(ny,) 垃圾箱沿第二维边缘.

除了以下事实外,关于数组的确切形状似乎还有一个错误 a>,我们看到返回的直方图数组的第一维为x,第二维为y.

但是, matplotlib始终希望y是第一个维度.因此,尽管plt.hist2d生成正确的图,但plt.pcolormesh需要数组的转置版本.

plt.pcolormesh(X,Y, counts.T)

比较plt.hist2dplt.pcolormesh的完整示例:

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(1,10,10)
y=np.linspace(6,9,10)

zbinsx= np.linspace(0,10,11)
zbinsy= np.linspace(5,10,6)

fig, (ax, ax2) = plt.subplots(ncols=2)
counts,xedges,yedges,d = ax.hist2d(x,y, bins=[zbinsx,zbinsy])
# counts has shape (10, 5)
X,Y = np.meshgrid(xedges,yedges)
mesh1 =ax2.pcolormesh(X,Y, counts.T)

plt.show()

I have the following code to plot a 2d histogram in pyplot:

#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt

MIN, MAX, num = .001, 5000, 500
minn=1
maxx=1000
zbins = 10 ** np.linspace(np.log10(MIN), np.log10(MAX), num)
x=np.linspace(100,600,50000)
y=np.linspace(0,500,50000)

fig1 = plt.figure(1)
counts1,xedges1,edges1,d=plt.hist2d(x,y,bins=zbins)
mesh1 = plt.pcolormesh(zbins, zbins, counts1)
plt.xlim([minn, maxx])
plt.ylim([minn, maxx])
plt.gca().set_xscale("log")
plt.gca().set_yscale("log")
plt.colorbar()

plt.show()

Apologies for my horrible variable naming!

Anyways, when I plot this, the histogram seems to have the x and y axes switched. I checked the matplotlib 2d hist documentation and I was sure that I had the x and y arguments in the right order, but I cannot for the life of me figure out where I'm going wrong. Any help would be greatly appreciated!

解决方案

The confusion comes from the fact that the returned counts array is not what you think it is.

plt.hist2d internally uses numpy.histogram2d to compute the two-dimensional histogram. The documentation states as return values:

H : ndarray, shape(nx, ny) The bi-dimensional histogram of samples x and y. Values in x are histogrammed along the first dimension and values in y are histogrammed along the second dimension.
xedges : ndarray, shape(nx,) The bin edges along the first dimension.
yedges : ndarray, shape(ny,) The bin edges along the second dimension.

Apart from the fact that there seems to be a mistake concerning the exact shape of the arrays, we see that the first dimension of the returned histogram array is x and the second y.

However, matplotlib always expects y to be the first dimenstion. Therefore, while plt.hist2d produces the correct plot, plt.pcolormesh needs a transposed version of the array.

plt.pcolormesh(X,Y, counts.T)

A full example, comparing plt.hist2d and plt.pcolormesh:

import numpy as np
import matplotlib.pyplot as plt

x=np.linspace(1,10,10)
y=np.linspace(6,9,10)

zbinsx= np.linspace(0,10,11)
zbinsy= np.linspace(5,10,6)

fig, (ax, ax2) = plt.subplots(ncols=2)
counts,xedges,yedges,d = ax.hist2d(x,y, bins=[zbinsx,zbinsy])
# counts has shape (10, 5)
X,Y = np.meshgrid(xedges,yedges)
mesh1 =ax2.pcolormesh(X,Y, counts.T)

plt.show()

这篇关于Matplotlib 2D直方图似乎已转置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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