Creating a 1D heat map from a line graph

后端 未结 1 1866
后悔当初
后悔当初 2021-02-10 00:26

Is it possible to create a 1D heat map from data in a line graph? i.e. I\'d like the highest values in y to represent the warmer colours in a heat map. I\'ve attached an example

1条回答
  •  别那么骄傲
    2021-02-10 01:01

    If we assume that the data is equally spaced, one may use an imshow plot to recreate the plot from the question.

    import matplotlib.pyplot as plt
    import numpy as np; np.random.seed(1)
    plt.rcParams["figure.figsize"] = 5,2
    
    x = np.linspace(-3,3)
    y = np.cumsum(np.random.randn(50))+6
    
    fig, (ax,ax2) = plt.subplots(nrows=2, sharex=True)
    
    extent = [x[0]-(x[1]-x[0])/2., x[-1]+(x[1]-x[0])/2.,0,1]
    ax.imshow(y[np.newaxis,:], cmap="plasma", aspect="auto", extent=extent)
    ax.set_yticks([])
    ax.set_xlim(extent[0], extent[1])
    
    ax2.plot(x,y)
    
    plt.tight_layout()
    plt.show()
    

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