简体   繁体   English

如何在图像 matplotlib 上绘制 pcolor?

[英]How to plot pcolor over image matplotlib?

I would like to plot some fake data for pcolor over a png image with matplotlib.我想用 matplotlib 在 png 图像上绘制 pcolor 的一些假数据。

In this code I am just drawing an arrow (I'm new to matplotlib):在这段代码中,我只是画了一个箭头(我是 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')

I just can't plot with pcolor over my png.我只是无法在我的 png 上使用 pcolor 进行绘图。 Can someone help me?有人能帮我吗?

If you create an Axes instance (for example with fig,ax=plt.subplots() ), you can easily plot the pcolor on there.如果您创建一个Axes实例(例如使用fig,ax=plt.subplots() ),您可以轻松地在那里绘制pcolor Make sure you make the pcolor transparent, so that you can see the imshow image underneath.确保使 pcolor 透明,以便您可以看到下面的imshow图像。

Here's an example, using the image from 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')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM