Matplotlib 热图:当热图在其上绘制时图像旋转
Matplotlib heatmap: Image rotated when heatmap plot over it
我正在尝试在图像之上绘制热图。
我做了什么:
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()
当我将它们绘制在一起时,图像会旋转,上下颠倒,如下所示:
有没有人有恢复旧照片的解决方案?
您需要设置两个 imshow
实例的 origin
。但是,您还需要更改 extent
中的 yedges
implot = plt.imshow(im,origin='upper')
...
extent = [xedges[0], xedges[-1], yedges[-1], yedges[0]]
plt.imshow(heatmap, extent=extent,alpha=.5,origin='upper')
我正在尝试在图像之上绘制热图。 我做了什么:
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()
当我将它们绘制在一起时,图像会旋转,上下颠倒,如下所示:
有没有人有恢复旧照片的解决方案?
您需要设置两个 imshow
实例的 origin
。但是,您还需要更改 extent
yedges
implot = plt.imshow(im,origin='upper')
...
extent = [xedges[0], xedges[-1], yedges[-1], yedges[0]]
plt.imshow(heatmap, extent=extent,alpha=.5,origin='upper')