Creating your own markers
my_marker = mpath.Path(np.asarray((u,v)).T, codes)
Shapes are described by the movements of a cursor. We use the following three types of instructions:
MOVETO: This instruction will move the cursor to the specified coordinates; no line is drawn.
LINETO: This will move the cursor to the specified coordinates, while drawing a line.
CLOSEPOLY: It won't do anything, it will close the path. Your path will be concluded by this instruction.
# In[114]:
import numpy as np
import matplotlib.path as mpath
from matplotlib import pyplot as plt
# In[115]:
shape_description = [
( 1., 2., mpath.Path.MOVETO),
( 1., 1., mpath.Path.LINETO),
( 2., 1., mpath.Path.LINETO),
( 2., -1., mpath.Path.LINETO),
( 1., -1., mpath.Path.LINETO),
( 1., -2., mpath.Path.LINETO),
(-1., -2., mpath.Path.LINETO),
(-1., -1., mpath.Path.LINETO),
(-2., -1., mpath.Path.LINETO),
(-2., 1., mpath.Path.LINETO),
(-1., 1., mpath.Path.LINETO),
(-1., 2., mpath.Path.LINETO),
( 0., 0., mpath.Path.CLOSEPOLY),
]
# In[116]:
u, v, codes = zip(*shape_description)
#The constructor for the Path object takes a list of coordinates and a list of instructions as inputs
my_marker = mpath.Path(np.asarray((u,v)).T, codes)
data = np.random.rand(8,8)
plt.scatter(data[:,0], data[:,1], c='0.75', marker=my_marker, s=64)
plt.show()

本文介绍如何使用Python的Matplotlib库创建自定义标记。通过组合坐标和指令,可以设计复杂的形状并将其用作数据点的标记。这为数据可视化提供了高度的定制性和创造性。
7852

被折叠的 条评论
为什么被折叠?



