gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:23:22 28 4
gpt4 key购买 nike

我正在学习 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com