python - imshow 和 histogram2d : can't get them to work

标签 python numpy matplotlib

我正在学习 Python,这是我的第一个问题。我已经阅读了与 imshow 用法相关的其他主题,但没有找到任何有用的信息。抱歉我的英语不好。

我在这里绘制了一组点,左图:

points (left) and image (right)

现在我想看点密度的图像,所以我用了imshowhistogram2d,我在前面得到了右边的图像链接。

图像与点的分布不对应。这怎么可能?我已经按照帮助中的说明进行操作,甚至更改了一些参数,但没有任何效果:(

代码是:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm

j, h, k = np.loadtxt("test.dat", usecols=(2, 4, 6), \
    unpack=True)

# límites
xmin = -0.5
xmax =  3.0
ymin = -0.5
ymax =  4.0

# colores
j_h = j - h
h_k = h - k

# no todas las estrellas son graficadas    
x1 = 0.5
y1 = 0.5
b  = 2.2
c  = y1 - b * x1

x = y = np.array([])

for xi, yi in zip(h_k, j_h):
    if xi < (yi - c) / b:
        x = np.append(x, xi)
        y = np.append(y, yi)

# gráfico
fig = plt.figure(figsize=(8, 7))

ax = fig.add_subplot(111)
#ax.plot(x, y, "go")
ax.set_xlabel(r"X", fontsize=14)
ax.set_ylabel(r"Y", fontsize=14)
ax.axis([xmin, xmax, ymin, ymax])

# imagen
rango = [[xmin, xmax], [ymin, ymax]]
binsx = int((xmax - xmin) / 0.05)
binsy = int((ymax - ymin) / 0.05)
binsxy = [binsx, binsy]

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

extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
cp = ax.imshow(H, interpolation='bilinear', extent=extent, cmap=cm.jet)
fig.colorbar(cp)

plt.show()

所用数据的链接在这里:

https://dl.dropbox.com/u/10411539/python/test.dat

感谢任何帮助!

最佳答案

尝试不同的插值,并转置矩阵使其在同一轴上:

cp = ax.imshow(H.transpose()[::-1], interpolation='nearest', extent=extent, cmap=cm.jet)

关于python - imshow 和 histogram2d : can't get them to work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11367683/

相关文章:

python - 加速我的 python 代码的技巧

Python 绘制不同的图形失败

python - "Proper"在 Python 中处理除 SIGINT 以外的信号的方法?

python - 将自定义字段添加到默认用户模型 - 如果扩展它,我应该进行迁移吗?

python - javascript __doPostBack 的网页抓取在 td 中包含 href

python - 根据其他数组的阈值创建新的 2d numpy 数组

python - 当运行locust --master和client locust --master-host时,locust不能运行api压力,为什么?

python - 使用 numpy 产生 2D 柏林噪声

python - 将 matplotlib.pyplot 导入为 plt

python - Seaborn python 改变绘图上的颜色系列