2012-01-04 86 views

回答

23

我會刪除勾號標籤並用patches替換文本。下面是執行此任務的一個簡單的例子:

import matplotlib.pyplot as plt 
import matplotlib.patches as patches 


# define where to put symbols vertically 
TICKYPOS = -.6 

fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.plot(range(10)) 

# set ticks where your images will be 
ax.get_xaxis().set_ticks([2,4,6,8]) 
# remove tick labels 
ax.get_xaxis().set_ticklabels([]) 


# add a series of patches to serve as tick labels 
ax.add_patch(patches.Circle((2,TICKYPOS),radius=.2, 
          fill=True,clip_on=False)) 
ax.add_patch(patches.Circle((4,TICKYPOS),radius=.2, 
          fill=False,clip_on=False)) 
ax.add_patch(patches.Rectangle((6-.1,TICKYPOS-.05),.2,.2, 
           fill=True,clip_on=False)) 
ax.add_patch(patches.Rectangle((8-.1,TICKYPOS-.05),.2,.2, 
           fill=False,clip_on=False)) 

這將導致如下圖所示:

enter image description here

關鍵是要設置clip_onFalse,否則patches軸外將不所示。補丁的座標和大小(半徑,寬度,高度等)將取決於您的座標軸在圖中的位置。例如,如果您正在考慮對子圖進行此操作,則需要對補丁放置進行敏感處理,以便不與其他任何軸重疊。您可能值得花時間調查Transformations,並在其他單位(軸,圖或顯示)中定義位置和大小。

如果您有要用於符號的特定圖像文件,則可以使用BboxImage類創建要添加到軸的藝術家,而不是修補程序。比如我做了下面的腳本一個簡單的圖標:

import matplotlib.pyplot as plt 

fig = plt.figure(figsize=(1,1),dpi=400) 
ax = fig.add_axes([0,0,1,1],frameon=False) 
ax.set_axis_off() 

ax.plot(range(10),linewidth=32) 
ax.plot(range(9,-1,-1),linewidth=32) 

fig.savefig('thumb.png') 

生產這一形象:

enter image description here

然後,我創建了我想要的刻度標籤大小我和位置的BboxImage想:

lowerCorner = ax.transData.transform((.8,TICKYPOS-.2)) 
upperCorner = ax.transData.transform((1.2,TICKYPOS+.2)) 

bbox_image = BboxImage(Bbox([lowerCorner[0], 
          lowerCorner[1], 
          upperCorner[0], 
          upperCorner[1], 
          ]), 
         norm = None, 
         origin=None, 
         clip_on=False, 
         ) 

注意到我如何使用transData轉變,從數據單位轉換爲顯示單元,whic h在Bbox的定義中是必需的。

現在我的圖像中讀出使用imread例程,並且它的結果(一個numpy的陣列)設置爲bbox_image數據和藝術家添加到軸:

bbox_image.set_data(imread('thumb.png')) 
ax.add_artist(bbox_image) 

這導致更新圖: enter image description here

如果直接使用圖像,確保導入所需的類和方法:

from matplotlib.image import BboxImage,imread 
from matplotlib.transforms import Bbox