pyplot: loglog() with base e

后端 未结 2 1955
故里飘歌
故里飘歌 2021-01-18 01:09

Python (and matplotlib) newbie here coming over from R, so I hope this question is not too idiotic. I\'m trying to make a loglog plot on a natural log scale. But after some

相关标签:
2条回答
  • 2021-01-18 01:37

    When plotting using plt.loglog you can pass the keyword arguments basex and basey as shown below.

    From numpy you can get the e constant with numpy.e (or np.e if you import numpy as np)

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Generate some data.
    x = np.linspace(0, 2, 1000)
    y = x**np.e
    
    plt.loglog(x,y, basex=np.e, basey=np.e)
    plt.show()
    

    Edit

    Additionally if you want pretty looking ticks you can use matplotlib.ticker to choose the format of your ticks, an example of which is given below.

    import numpy as np
    
    import matplotlib.pyplot as plt
    import matplotlib.ticker as mtick
    
    x = np.linspace(1, 4, 1000)
    
    y = x**3
    
    fig, ax = plt.subplots()
    
    ax.loglog(x,y, basex=np.e, basey=np.e)
    
    def ticks(y, pos):
        return r'$e^{:.0f}$'.format(np.log(y))
    
    ax.xaxis.set_major_formatter(mtick.FuncFormatter(ticks))
    ax.yaxis.set_major_formatter(mtick.FuncFormatter(ticks))
    
    plt.show()
    

    Plot

    0 讨论(0)
  • 2021-01-18 01:43

    It can also works for semilogx and semilogy to show them in e and also change their name.

    import matplotlib.ticker as mtick
    fig, ax = plt.subplots()
    def ticks(y, pos):
        return r'$e^{:.0f}$'.format(np.log(y))
    
    plt.semilogy(Time_Series, California_Pervalence ,'gray', basey=np.e  )
    ax.yaxis.set_major_formatter(mtick.FuncFormatter(ticks))
    
    plt.show()
    

    Take a look at the image.enter image description here

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