2012-01-04 46 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