5
votes

This is a weird one, hope someone can help me. I'm trying to do a density plot of my data with matplotlib heatmap, but somehow my data is getting reversed/rotated in a strange way. I have a scatterplot and then I'm binning those points for density, but the image is not at all what should be coming out. For example, here's the original scatter plot, which has the correct orientation:

original scatter plot

Then here's my heatmap (notice that the structure is rotated 90 degrees counterclockwise from above, but that the axis data is correct... the numbers on the axes are generated automatically from the data, so if you simply reverse the axes the picture comes out correctly but the numbers are all off):

heatmap with wrong orientation, but correct axes

I just can't see how this could be, since the data parsing routine is identical to what it was before when I just generated the scatter plot. It has to be something in the way the heatmap plot is coded, I would think, but I don't see where the breakdown could be. I've already tried accounting for the built-in origin placement (upper-left corner) for the heatmap, that doesn't solve it. The code is as follows (first all the data parsing):

import numpy as np
from numpy import ndarray
import matplotlib.pyplot as plt
import matplotlib
import atpy
from pylab import *

twomass = atpy.Table()

twomass.read('/Raw_Data/IRSA_downloads/2MASS_GCbox2.tbl')


hmag = list([twomass['h_m']])
jmag = list([twomass['j_m']])
hmag = np.array(hmag)
jmag = np.array(jmag)
colorjh = np.array(jmag - hmag)

idx_c = (colorjh > -1) & (colorjh < 6)  #manipulate desired color quantities here
idx_h = (hmag > 8) & (hmag < 18)
idx = idx_c & idx_h

Now here's the heatmap code:

heatmap, xedges, yedges = np.histogram2d(colorjh[idx], hmag[idx], bins=500)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]

plt.clf()
plt.imshow(heatmap, extent=extent)

plt.xlabel('Color(J-H)', fontsize=15)           #adjust axis labels here
plt.ylabel('Magnitude (H)', fontsize=15)

plt.gca().invert_yaxis()

plt.legend(loc=2)
plt.title('CMD for Galactic Center (2MASS)', fontsize=20)
plt.grid(True)
plt.show()

I'm pretty new to Python, so the less jargony the explanation, the more likely I'll be able to put it to good use. Thanks for any help y'all can provide.

1

1 Answers

7
votes

Your issue seems to be with the way that np.histogram2d works. From the documentation:

Please note that the histogram does not follow the Cartesian convention where x values are on the abcissa and y values on the ordinate axis. Rather, x is histogrammed along the first dimension of the array (vertical), and y along the second dimension of the array (horizontal). This ensures compatibility with histogramdd.

Using

extent = [yedges[0], yedges[-1], xedges[-1], xedges[0]]

instead should give you what you expect. Reverse colorjh[idx] and hmag[idx] in your histogram2d call as well if you want to keep the same orientation as your scatterplot.