Matplotlib heatmap: Image rotated when heatmap plot over it

2.7k views Asked by At

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?

1

There are 1 answers

0
tmdavison On BEST ANSWER

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