Matplotlib reversal temperature change?

This is strange, I hope someone can help me. I am trying to plot the density of my data using matplotlib heatmap, but for some reason my data is changing / rotating in a weird way. I have a scatter chart and then I use these points for density, but the image is not at all what should appear. For example, here is the original scatter plot that has the correct orientation:

original scatter plot

Then here is my heatmap (note that the structure rotates 90 degrees counterclockwise from above, but the axis data is correct ... the numbers on the axes are automatically generated from the data, so if you just change the axes, the image is correct, but the numbers all off):

heatmap with wrong orientation, but correct axes

I just donโ€™t see how this can be, because the procedure for parsing the data is identical to the one that was before when I just generated the scatter plot. I think it should be something like the heat map graph is encoded, but I donโ€™t see where the breakdown can occur. I have already tried accounting for the built-in placement (upper left corner) for a heatmap, which does not solve the problem. The code is as follows (first, the entire parsing of the data):

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 is the card 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(JH)', 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 am new to Python, so the fewer the explanations, the more likely I will be able to use it for good use. Thanks for any help you can provide.

+4
source share
1 answer

Your problem seems to be related to how np.histogram2d works. From the documentation :

Note that the histogram does not correspond to the Cartesian convention, where the x values โ€‹โ€‹are located on the abcissa and y values โ€‹โ€‹along the ordinate. Rather, x is plotted along the first dimension of the array (vertical) and y is the second dimension of the array (horizontal). This provides compatibility with histogramdd .

Using

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

instead should give you what you expect. The inverse colorjh[idx] and hmag[idx] in your histogram2d call also if you want to keep the same orientation as your scatter plot.

+7
source

All Articles