Matplotlib plotting a single line that continuously changes color

后端 未结 1 1599
温柔的废话
温柔的废话 2020-12-31 06:27

I would like to plot a curve in the (x,y) plane, where the color of the curve depends on a value of another variable T. x is a 1D numpy array, y is a 1D numpy array.

1条回答
  •  执笔经年
    2020-12-31 07:13

    One idea could be to set the color using color=(R,G,B) then split your plot into n segments and continuously vary either one of the R, G or B (or a combinations)

    import pylab as plt
    import numpy as np
    
    # Make some data
    n=1000
    x=np.linspace(0,100,n)
    y=np.sin(x)
    
    # Your coloring array
    T=np.linspace(0,1,np.size(x))**2
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    # Segment plot and color depending on T
    s = 10 # Segment length
    for i in range(0,n-s,s):
        ax.plot(x[i:i+s+1],y[i:i+s+1],color=(0.0,0.5,T[i]))
    

    Hope this is helpful

    0 讨论(0)
提交回复
热议问题