matplotlib log axis: display powers of 10 only

后端 未结 2 1968
星月不相逢
星月不相逢 2021-01-22 09:57

I have a log-log plot with range of x-axis from 10^9 to 10^12. (This is my first time posting so I am unable to post an image of my plot)

I would like to change the x an

相关标签:
2条回答
  • 2021-01-22 10:43

    LogFormatterExponent(base=10.0, labelOnlyBase=True) works as expected.

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.ticker
    
    x = 10**np.linspace(8.5,12.6)
    y = np.sin(x)
    
    fig,ax = plt.subplots()
    ax.scatter(x,y)
    ax.set_xscale('log')
    ax.set_xlabel("Quantity [$10^{x}]$")
    
    logfmt = matplotlib.ticker.LogFormatterExponent(base=10.0, labelOnlyBase=True)
    ax.xaxis.set_major_formatter(logfmt)
    
    plt.show()
    

    0 讨论(0)
  • 2021-01-22 10:49

    Would it be so easy as to plot the X data in linear scale on a semilog plot?

    plt.semilogy(np.log10(x), y)
    

    Then you'll have the X scale as powers of ten.

    For example:

    import numpy as np
    import matplotlib.pyplot as plt
    
    # create some data
    x = 10**np.linspace(0,9,100)
    y = np.sqrt(100 + x)
    
    # plot the figure
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.semilogy(np.log10(x), y)
    
    ax.set_xlabel("$10^x$")
    ax.set_ylabel("$\sqrt{100 + x}$")
    

    This gives:

    enter image description here

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