![](/img/trans.png)
[英]How to plot a numpy array over a pcolor image in matplotlib?
[英]How to plot pcolor over image 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 进行绘图。 有人能帮我吗?
如果您创建一个Axes
实例(例如使用fig,ax=plt.subplots()
),您可以轻松地在那里绘制pcolor
。 确保使 pcolor 透明,以便您可以看到下面的imshow
图像。
这是一个示例,使用此处的图像
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.