如何在图像matplotlib上绘制pcolor?

时间:2016-04-26 09:33:08

标签: python matplotlib

我想用matplotlib在png图像上绘制pcolor的一些假数据。

在这段代码中,我只是画了一个箭头(我是matplotlib的新手):

import matplotlib.pyplot as plt
import pylab
im = plt.imread('pitch.png')
implot = plt.imshow(im)


plt.annotate("",
        xy=(458, 412.2), xycoords='data',
        xytext=(452.8, 363.53), textcoords='data',
        arrowprops=dict(arrowstyle="<-",
                        connectionstyle="arc3"), 
        )

pylab.savefig('foo.png')

我无法在png上用pcolor绘图。有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

如果您创建Axes实例(例如使用fig,ax=plt.subplots()),则可以在那里轻松绘制pcolor。确保使pcolor透明,以便您可以看到下面的imshow图像。

以下是使用here

中的图片的示例
import matplotlib.pyplot as plt
import numpy as np

im = plt.imread('stinkbug.png')

# Create Figure and Axes objects
fig,ax = plt.subplots(1)

# display the image on the Axes
implot = ax.imshow(im)

# Some dummy data to use in pcolor
x = np.arange(im.shape[1])
y = np.arange(im.shape[0])
X,Y = np.meshgrid(x,y)
data = X+Y

# plot the pcolor on the Axes. Use alpha to set the transparency
p=ax.pcolor(X,Y,data,alpha=0.5,cmap='viridis')

# Note I changed your coordinates so the arrow would fit on this image
ax.annotate("",
        xy=(458, 150), xycoords='data',
        xytext=(452.8, 250), textcoords='data',
        arrowprops=dict(arrowstyle="<-",
                        connectionstyle="arc3"), 
        )

# Add a colorbar for the pcolor field
fig.colorbar(p,ax=ax)

plt.savefig('foo.png')

enter image description here

相关问题