栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

带有行颜色渐变和颜色栏的python matplotlib

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

带有行颜色渐变和颜色栏的python matplotlib

看看在multicolored_line例如在Matplotlib画廊和dpsanders’
colorline笔记本

import numpy as npimport matplotlib.pyplot as pltimport matplotlib.collections as mcolldef multicolored_lines():    """    http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb    http://matplotlib.org/examples/pylab_examples/multicolored_line.html    """    x = np.linspace(0, 4. * np.pi, 100)    y = np.sin(x)    fig, ax = plt.subplots()    lc = colorline(x, y, cmap='hsv')    plt.colorbar(lc)    plt.xlim(x.min(), x.max())    plt.ylim(-1.0, 1.0)    plt.show()def colorline(        x, y, z=None, cmap='copper', norm=plt.Normalize(0.0, 1.0),        linewidth=3, alpha=1.0):    """    http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb    http://matplotlib.org/examples/pylab_examples/multicolored_line.html    Plot a colored line with coordinates x and y    Optionally specify colors in the array z    Optionally specify a colormap, a norm function and a line width    """    # Default colors equally spaced on [0,1]:    if z is None:        z = np.linspace(0.0, 1.0, len(x))    # Special case if a single number:    # to check for numerical input -- this is a hack    if not hasattr(z, "__iter__"):        z = np.array([z])    z = np.asarray(z)    segments = make_segments(x, y)    lc = mcoll.LineCollection(segments, array=z, cmap=cmap, norm=norm,        linewidth=linewidth, alpha=alpha)    ax = plt.gca()    ax.add_collection(lc)    return lcdef make_segments(x, y):    """    Create list of line segments from x and y coordinates, in the correct format    for LineCollection: an array of the form numlines x (points per line) x 2 (x    and y) array    """    points = np.array([x, y]).T.reshape(-1, 1, 2)    segments = np.concatenate([points[:-1], points[1:]], axis=1)    return segmentsmulticolored_lines()


请注意,调用

plt.plot
数百次往往会降低性能。使用
LineCollection
来构建多色线段要快得多。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/647161.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号