def _colorline(ax, x, y, color = (0, 0, 0), **kwargs):
'''
Plots the curve `y(x)` with linearly increasing alpha.
Adapted from `http://nbviewer.jupyter.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb`_.
'''
# A bit hacky... But there doesn't seem to be
# an easy way to get the hex code for a named color...
if isinstance(color, string_types):
if color.startswith("#"):
hex = color[1:]
else:
if len(color) == 1:
if color == 'k':
color = 'black'
elif color == 'r':
color = 'red'
elif color == 'b':
color = 'blue'
elif color == 'g':
color = 'green'
elif color == 'y':
color = 'yellow'
elif color == 'w':
color = 'white'
else:
# ?!
color = 'black'
hex = matplotlib.colors.cnames[color.lower()][1:]
r, g, b = tuple(int(hex[i:i+2], 16) / 255. for i in (0, 2, 4))
else:
r, g, b = color
colors = [(r, g, b, i) for i in np.linspace(0, 1, 3)]
cmap = LinearSegmentedColormap.from_list('alphacmap', colors, N = 1000)
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
lc = LineCollection(segments, array = np.linspace(0.0, 1.0, len(x)),
cmap = cmap, norm = pl.Normalize(0.0, 1.0), **kwargs)
ax.add_collection(lc)
return lc
评论列表
文章目录