python - cmap for use with line plot -
i have list of color values (in either of formats: hex ('#ffffff') or rgb (255,255,255) if helps). these colors correspond explicitly line segment between points. plot line collection of line segments via:
import matplotlib.pyplot plt import itertools colors = itertools.cycle('#ffffff', '#ffffff', '#ff0320', '#452143', ...) t = (0, 1, 2, 3, ...) var1 = (43, 15, 25, 9, ...) ax = plt.subplot2grid((3,1), (0,0), colspan=3, rowspan=1) ps = [(t,var1) (t,var1) in zip(t, val)] start, end in zip(ps[:-1], ps[1:]): t, var1 = zip(start, end) c = next(colors) ax.plot(t, var1, color=c)
however since have color each point prefer set cmap plot. how might accomplish converting list of colors cmap can use when plotting line?
as tcaswell says, use linecollection
this:
import numpy np matplotlib import pyplot plt matplotlib.collections import linecollection # random walk xy = np.cumsum(np.random.randn(1000, 2), axis=0) z = np.linspace(0, 1, 1000) lc = linecollection(zip(xy[:-1], xy[1:]), array=z, cmap=plt.cm.hsv) fig, ax = plt.subplots(1, 1) ax.add_collection(lc) ax.margins(0.1) plt.show()
Comments
Post a Comment