我在学习Python,这是我的第一个问题。我已阅读其他与imshow的使用相关的主题,但没有找到任何有用的内容。对不起,我的英语不好。imshow和histogram2d:无法让他们工作
我策划了一组点的位置,左边图形:
points (left) and image (right)
现在我想看看点的密度的图像,所以我用imshow和histogram2d ,并在前面的链接中将图片显示在右侧。
该图像不对应点的分布。这怎么可能?我紧跟在帮助中的说明,甚至改变了一些参数,但没有奏效:(
的代码是:
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
任何帮助表示赞赏
指定'起源='lower''为imshow功能 – gcalmettes 2012-07-06 18:39:01
@gcalmettes:这并没有工作:( – skytux 2012-07-06 19:21:30
现在它的工作,但设置H.transpose()谢谢!。 – skytux 2012-07-06 19:36:05