Square root scale using matplotlib/python

后端 未结 4 1781
孤独总比滥情好
孤独总比滥情好 2020-12-21 05:55

I want to make a plot with square root scale using Python:

However, I have no idea how to make it. Matplotlib allows to make log scale but in this case I ne

4条回答
  •  难免孤独
    2020-12-21 06:03

    Matplotlib now offers a powlaw norm. Thus setting power to 0.5 should do the trick!

    C.f. Matplotlib Powerlaw norm

    And their example:

    """
    Demonstration of using norm to map colormaps onto data in non-linear ways.
    """
    
    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.colors as colors
    from matplotlib.mlab import bivariate_normal
    
    N = 100
    X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)]
    
    '''
    PowerNorm: Here a power-law trend in X partially obscures a rectified
    sine wave in Y. We can remove gamma to 0.5 should do the trick using  PowerNorm.
    '''
    X, Y = np.mgrid[0:3:complex(0, N), 0:2:complex(0, N)]
    Z1 = (1 + np.sin(Y * 10.)) * X**(2.)
    
    fig, ax = plt.subplots(2, 1)
    
    pcm = ax[0].pcolormesh(X, Y, Z1, norm=colors.PowerNorm(gamma=1./2.),
                           cmap='PuBu_r')
    fig.colorbar(pcm, ax=ax[0], extend='max')
    
    pcm = ax[1].pcolormesh(X, Y, Z1, cmap='PuBu_r')
    fig.colorbar(pcm, ax=ax[1], extend='max')
    fig.show()
    

提交回复
热议问题