我想用 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 绘图。有人可以帮助我吗?
请您参考如下方法:
如果您创建一个 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')
