2010-02-23 20 views

回答

21

我不相信matplotlib可以自定義這樣的標記。請參閱here以瞭解您所需的自定義級別。

作爲一種替代方法,我編寫了這個使用figimage將圖像放置在線點位置的kludge。

import matplotlib.pyplot as plt 
import matplotlib.image as image 

# constants 
dpi = 72; imageSize = (32,32) 
# read in our png file 
im = image.imread('smile.png') 

fig = plt.figure(dpi=dpi) 
ax = fig.add_subplot(111) 
# plot our line with transparent markers, and markersize the size of our image 
line, = ax.plot((1,2,3,4),(1,2,3,4),"bo",mfc="None",mec="None",markersize=imageSize[0] * (dpi/ 96)) 
# we need to make the frame transparent so the image can be seen 
# only in trunk can you put the image on top of the plot, see this link: 
# http://www.mail-archive.com/[email protected]/msg14534.html 
ax.get_frame().set_alpha(0) 
ax.set_xlim((0,5)) 
ax.set_ylim((0,5)) 

# translate point positions to pixel positions 
# figimage needs pixels not points 
line._transform_path() 
path, affine = line._transformed_path.get_transformed_points_and_affine() 
path = affine.transform_path(path) 
for pixelPoint in path.vertices: 
    # place image at point, centering it 
    fig.figimage(im,pixelPoint[0]-imageSize[0]/2,pixelPoint[1]-imageSize[1]/2,origin="upper") 

plt.show() 

alt text http://i50.tinypic.com/war39w.png

+2

AttributeError:'AxesSubplot'對象沒有屬性'get_frame' – erogol 2014-11-16 15:45:29

+0

查看答案t處理get_frame錯誤的gillespie – Joel 2016-02-20 04:33:19

5

馬克的回答繼。我只是認爲我會添加到這一點,因爲我試圖運行這個,它做我想要的,除了實際顯示圖形上的圖標。也許matplotlib改變了一些東西。它已有 4年。

的代碼行,上面寫着:

ax.get_frame().set_alpha(0) 

似乎並沒有工作,但是

ax.patch.set_alpha(0) 

確實工作。

+0

http://matplotlib.org/faq/howto_faq.html – 2015-03-20 06:29:38