我想用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')


我只是无法用pcolor绘制我的png。有人能帮我吗?

最佳答案

如果创建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')


python - 如何在图像matplotlib上绘制pcolor?-LMLPHP

10-06 06:43