2016-04-26 2 views
2

Я хотел бы построить некоторые поддельные данные для pcolor над png-изображением с помощью matplotlib.Как построить pcolor над изображением 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') 

Я просто не могу построить с pcolor над моей PNG. Кто-нибудь может мне помочь?

ответ

1

Если вы создаете экземпляр 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

Смежные вопросы