imshow and histogram2d: cant get them to work

2024/10/14 7:19:31

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:

points (left) and image (right)

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

The code is:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cmj, 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 * x1x = 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

Any help is appreciated!

Answer

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)
https://en.xdnf.cn/q/69438.html

Related Q&A

3D Waterfall Plot with Colored Heights

Im trying to visualise a dataset in 3D which consists of a time series (along y) of x-z data, using Python and Matplotlib.Id like to create a plot like the one below (which was made in Python: http://a…

python xlsxwriter: Keep header in excel when adding a table

I have a panda dataframe that I write to a xslx file, and would like to add a table over that data. I would also like to keep the headers that I have already written, instead of adding them again. Is t…

Unexpected tokens in !DOCTYPE html in PyCharm Community Edition

I am new in using PyCharm but I am loving it gradually. I am getting a red underline on <!DOCTYPE html> and the error is "Unexpected Token".Why PyCharm shows it? I cant understand.

Indexing a numpy array with a numpy array of indexes [duplicate]

This question already has answers here:Indexing a numpy array with a list of tuples(2 answers)Index multidimensional array with index array(1 answer)Closed 5 years ago.I have a 3D numpy array data and …

Input redirection with python

I have the following program to test input redirection in Python.a = int(raw_input("Enter a number: ")) b = raw_input("Enter a string: ") print "number entered = ", a prin…

TypeError: can only concatenate str (not numpy.int64) to str

I want to print the variable based on the index number based on the following dataset:Here I used the following code:import pandas as pdairline = pd.read_csv("AIR-LINE.csv")pnr = input("…

Saving scatterplot animations with matplotlib produces blank video file

I am having a very similar problem to this questionbut the suggested solution doesnt work for me. I have set up an animated scatter plot using the matplotlib animation module. This works fine when it i…

How to group near-duplicate values in a pandas dataframe?

If there are duplicate values in a DataFrame pandas already provides functions to replace or drop duplicates. In many experimental datasets on the other hand one might have near duplicates. How can one…

python looping and creating new dataframe for each value of a column

I want to create a new dataframe for each unique value of station.I tried below which gives me only last station data updated in the dataframe = tai_new.itai[station].unique() has 500 values.for i in t…

How to put more whitespace around my plots?

I have a figure that contains two subplots in two rows and one column like so:fig, (ax1, ax2) = subplots(nrows=2,ncols=1, )The two subplots are pie charts, therefore I want their axes to be square. Aft…