【问题标题】:Python matplotlib ColorfunctionPython matplotlib 颜色函数
【发布时间】:2016-06-20 15:50:43
【问题描述】:

我想在 python 中使用类似于MathematicaColorFunction

换句话说,我想调用pyplot.plot(x, y, color=c),其中c是一个向量,定义了每个数据点的颜色。

有没有办法使用 matplotlib 库来实现这一点?

【问题讨论】:

    标签: python matplotlib colors


    【解决方案1】:

    据我所知,Matplotlib 中没有等价物,但我们可以通过以下两个步骤得到类似的结果:绘制不同颜色的点并绘制线条。

    这是一个演示。


    源代码,

    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib import cm
    import random
    
    fig, ax = plt.subplots()
    
    nrof_points = 100 
    x = np.linspace(0, 10, nrof_points)
    y = np.sin(x)
    colors = cm.rainbow(np.linspace(0, 1, nrof_points))     # generate a bunch of colors
    
    # draw points
    for idx, point in enumerate(zip(x, y)):
        ax.plot(point[0], point[1], 'o', color=colors[idx], markersize=10)
    
    # draw the line
    ax.plot(x, y, 'k')
    plt.grid()
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2012-04-11
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 2014-10-19
      • 2017-06-19
      • 2019-04-11
      • 2014-10-19
      相关资源
      最近更新 更多