Imshow and histogram2d: can't make them work

I am learning Python and this is my first question. I read other topics related to using imshow , but did not find anything useful. Sorry for my bad english.

I drew a set of dots here, left graphic:

dots (left) and image (right)

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

The image does not match the distribution of points. How is this possible? I followed the instructions in the help and even changed some parameters, but nothing worked :(

The code:

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

Links for the data used:

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

Any help is appreciated!

+6
source share
2 answers

Try another interpolation and drag the matrix to get it on the same axis:

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

Is this what you want to get? You can use pcolor (and pcolormesh) if you want to pass the x and y coordinates.

 import urllib import numpy as np import matplotlib.pyplot as plt f = urllib.urlopen('https://dl.dropbox.com/u/10411539/python/test.dat') j, h, k = np.loadtxt(f, usecols=(2, 4, 6), \ unpack=True) j, h, k j_h = j - h h_k = h - k H, xedges, yedges = np.histogram2d(j_h, h_k, bins=100) plt.pcolor(xedges, yedges, H) 

Example of pcolor using histogram2d

For imshow you need to undo the first dimension because imshow uses zero rows, column indices for x, y. Top down drawing.

 plt.imshow(H[::-1,:], extent=(0,5, 0,2.5)) # typed in extent by hand. 

imshow example

+4
source

Source: https://habr.com/ru/post/919892/


All Articles