如何在图像matplotlib上绘制pcolor?

2024-05-06 08:24:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是一个新的绘图程序:

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绘制。有人能帮我吗?在


Tags: 数据图像import程序绘图datapngmatplotlib
1条回答
网友
1楼 · 发布于 2024-05-06 08:24:22

如果您创建一个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

相关问题 更多 >