Search code examples
pythonmatplotlibaxis-labels

Matplotlib log scale formatting


With matplotlib when a log scale is specified for an axis, the default method of labeling that axis is with numbers that are 10 to a power eg. 10^6. Is there an easy way to change all of these labels to for example: 1, 5, 10, 20?


Solution

  • Found in this thread

    import matplotlib.pyplot as pl
    from matplotlib.ticker import ScalarFormatter
    
    fig = pl.figure()
    ax = fig.add_subplot(111)
    
    ax.set_xscale('log')
    ax.set_xticks([1,2,5,10])
    ax.get_xaxis().set_major_formatter(ScalarFormatter())
    ax.set_xlim([1., 10.])
    

    enter image description here