imshow和histogram2d:无法使它们正常工作 [英] imshow and histogram2d: can't get them to work

查看:79
本文介绍了imshow和histogram2d:无法使它们正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Python,这是我的第一个问题.我已经阅读了与 imshow 的用法有关的其他主题,但没有发现有用的信息.对不起,我的英语不好.

I'm learning Python and this is my first question here. I've read other topics related to the usage of imshow but didn't find anything useful. Sorry for my bad English.

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

I have plotted a set of points here, left graphic:

点(左)和图像(右)

现在我想查看点密度的图像,所以我使用了 imshow histogram2d ,在上一幅图像中,我将图像移到了右侧链接.

Now I'd like to see an image of the density of points, so I used imshow and histogram2d, and I got the image to the right in the previous link.

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

The image doesn't correspond to the distribution of points. How is this possible? I've followed the instructions in the help and even changed some parameters but nothing worked :(

代码是:

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

使用的数据链接在这里:

The links for the data used is here:

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

感谢您的帮助!

推荐答案

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

Try different interpolation, and transpose the matrix to get it in the same axis:

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

这篇关于imshow和histogram2d:无法使它们正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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