Matplotlib: imshow with second y axis

前端 未结 1 1446
耶瑟儿~
耶瑟儿~ 2021-01-18 06:59

I\'m trying to plot a two-dimensional array in matplotlib using imshow(), and overlay it with a scatterplot on a second y axis.

oneDim = np.array([0.5,1,2.5,         


        
1条回答
  •  清歌不尽
    2021-01-18 07:25

    Following the GitHub discussion which Thomas Kuehn has pointed at, the issue has been fixed few days ago. In the absence of a readily available built, here's a fix using the aspect='auto' property. In order to get nice regular boxes, I adjusted the figure x/y using the array dimensions. The axis autoscale feature has been used to remove some additional white border.

    oneDim = np.array([0.5,1,2.5,3.7])
    twoDim = np.random.rand(8,4)
    
    plt.figure(figsize=(twoDim.shape[1]/2,twoDim.shape[0]/2))
    ax1 = plt.gca()
    
    ax1.imshow(twoDim, cmap='Purples', interpolation='nearest', aspect='auto')
    ax1.set_xticks(np.arange(0,twoDim.shape[1],1))
    ax1.set_yticks(np.arange(0,twoDim.shape[0],1))
    ax1.set_yticklabels(np.arange(0,twoDim.shape[0],1))
    ax1.grid()
    
    ax2 = ax1.twinx()
    
    #Required to remove some white border
    ax1.autoscale(False)
    ax2.autoscale(False)
    

    Result:

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