. , (mark_align_*). verts ( 2- Nx2, (0, 0)). , ,
from matplotlib import markers
from matplotlib.path import Path
def align_marker(marker, halign='center', valign='middle',):
"""
create markers with specified alignment.
Parameters
----------
marker : a valid marker specification.
See mpl.markers
halign : string, float {'left', 'center', 'right'}
Specifies the horizontal alignment of the marker. *float* values
specify the alignment in units of the markersize/2 (0 is 'center',
-1 is 'right', 1 is 'left').
valign : string, float {'top', 'middle', 'bottom'}
Specifies the vertical alignment of the marker. *float* values
specify the alignment in units of the markersize/2 (0 is 'middle',
-1 is 'top', 1 is 'bottom').
Returns
-------
marker_array : numpy.ndarray
A Nx2 array that specifies the marker path relative to the
plot target point at (0, 0).
Notes
-----
The mark_array can be passed directly to ax.plot and ax.scatter, e.g.::
ax.plot(1, 1, marker=align_marker('>', 'left'))
"""
if isinstance(halign, (str, unicode)):
halign = {'right': -1.,
'middle': 0.,
'center': 0.,
'left': 1.,
}[halign]
if isinstance(valign, (str, unicode)):
valign = {'top': -1.,
'middle': 0.,
'center': 0.,
'bottom': 1.,
}[valign]
# Define the base marker
bm = markers.MarkerStyle(marker)
# Get the marker path and apply the marker transform to get the
# actual marker vertices (they should all be in a unit-square
# centered at (0, 0))
m_arr = bm.get_path().transformed(bm.get_transform()).vertices
# Shift the marker vertices for the specified alignment.
m_arr[:, 0] += halign / 2
m_arr[:, 1] += valign / 2
return Path(m_arr, bm.get_path().codes)
, ,
ax.plot(d1, 1, marker=align_marker('>', halign='left'), ms=20,
clip_on=False, color='k', transform=ax.get_xaxis_transform())
ax.plot(d2, 1, marker=align_marker('<', halign='right'), ms=20,
clip_on=False, color='k', transform=ax.get_xaxis_transform())
ax.scatter,
ax.scatter(d1, 1, 200, marker=align_marker('>', halign='left'),
clip_on=False, color='k', transform=ax.get_xaxis_transform())
ax.scatter(d2, 1, 200, marker=align_marker('<', halign='right'),
clip_on=False, color='k', transform=ax.get_xaxis_transform())
transform=ax.get_xaxis_transform() , (1 - ), .
, , (ax.plot vs. ax.scatter) ( ). !
!