问题描述
我不想用单词或数字作为x轴的刻度标签,而是要绘制一个简单的图形(由线和圆组成)作为每个x刻度的标签.这可能吗?如果是这样,在matplotlib中进行处理的最佳方法是什么?
Instead of words or numbers being the tick labels of the x axis, I want to draw a simple drawing (made of lines and circles) as the label for each x tick. Is this possible? If so, what is the best way to go about it in matplotlib?
推荐答案
我将删除刻度线标签,并用补丁.这是执行此任务的简短示例:
I would remove the tick labels and replace the text with patches. Here is a brief example of performing this task:
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))
这将导致下图:
将clip_on
设置为False
是关键,否则将不显示轴外的patches
.面片的坐标和大小(半径,宽度,高度等)将取决于轴在图中的位置.例如,如果您考虑对子图进行此操作,则需要对色标的位置敏感,以免与其他轴重叠.您可能值得花时间研究变换,并定义其他单元中的位置和大小(轴,图形或显示).
It is key to set clip_on
to False
, otherwise patches
outside the axes will not be shown. The coordinates and sizes (radius, width, height, etc.) of the patches will depend on where your axes is in the figure. For example, if you are considering doing this with subplots, you will need to be sensitive of the patches placement so as to not overlap any other axes. It may be worth your time investigating Transformations, and defining the positions and sizes in an other unit (Axes, Figure or display).
如果有要用于符号的特定图像文件,则可以使用BboxImage
类创建要添加到轴而不是色块的艺术家.例如,我使用以下脚本制作了一个简单的图标:
If you have specific image files that you want to use for the symbols, you can use the BboxImage
class to create artists to be added to the axes instead of patches. For example I made a simple icon with the following script:
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')
生成此图像:
然后我在想要刻度线标签和想要的大小的位置创建了一个BboxImage:
Then I created a BboxImage at the location I want the tick label and of the size I want:
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
转换将数据单元转换为显示单元,这在Bbox
的定义中是必需的.
Noticed how I used the transData
transformation to convert from data units to display units, which are required in the definition of the Bbox
.
现在,我使用imread
例程读取图像,并将其结果(一个numpy数组)设置为bbox_image
的数据,并将艺术家添加到坐标轴:
Now I read in the image using the imread
routine, and set it's results (a numpy array) to the data of bbox_image
and add the artist to the axes:
bbox_image.set_data(imread('thumb.png'))
ax.add_artist(bbox_image)
这将产生一个更新的数字:
This results in an updated figure:
如果您直接使用图像,请确保导入所需的类和方法:
If you do directly use images, make sure to import the required classes and methods:
from matplotlib.image import BboxImage,imread
from matplotlib.transforms import Bbox
这篇关于如何使用matplotlib使图的xtick标签成为简单图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!