1

I am trying to plot a heatmap on top of an image. What I did:

import matplotlib.pyplot as plt
import numpy as np
import numpy.random
import urllib
#downloading an example image
urllib.urlretrieve("http://tekeye.biz/wp-content/uploads/2013/01/small_playing_cards.png", "/tmp/cards.png")
#reading and plotting the image
im = plt.imread('/tmp/cards.png')
implot = plt.imshow(im)
#generating random data for the histogram
x=numpy.random.normal(500, 100, size=1000)
y=numpy.random.normal(100, 50, size=1000)
heatmap, xedges, yedges = np.histogram2d(x, y, bins=50)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.imshow(heatmap, extent=extent,alpha=.5)
plt.show()

When I plot them together, the image get's rotated, up-side down as in:

enter image description here

Does anyone have a solution for having the old picture back?

MajorYel
  • 379
  • 1
  • 5
  • 10
  • 2
    You probably have your `imshow` image orientation defaulting to something other that what your image assumes. Try imshow keyword `origin` and set it to `"upper"` or `"lower"`. Be careful that the plot and image have consistent orientations -- it's easy to get these wrong. See, for example, http://stackoverflow.com/questions/21648379/python-matplotlib-quiver-wrong-orientation Usually images default to origin in the upper left, while plots have the origin in the lower left. – tom10 Jun 15 '15 at 16:16

1 Answers1

2

you need to set the origin of both the imshow instances. But, you also need to change the yedges around in your extent

implot = plt.imshow(im,origin='upper')
...
extent = [xedges[0], xedges[-1], yedges[-1], yedges[0]]
plt.imshow(heatmap, extent=extent,alpha=.5,origin='upper')

enter image description here

tmdavison
  • 64,360
  • 12
  • 187
  • 165