Search code examples
pythonmatplotliblabelaxismagnitude

Python Pylab, how to alter the size of the label specifying the magnitude of the axes


I am attempting to plot differential cross-sections of nuclear decays and so the magnitudes of the y-axis are around 10^-38 (m^2) pylab as default plots the axis as 0.0,0.2,0.4... etc and has a '1e-38' at the top of the y-axis.

I need to increase the font size of just this little bit, I have tried adjusting the label size

py.tick_params(axis='y', labelsize=20)

but this only adjusts the labels 0.0,0.2,0.4....

Many thanks for all help


Solution

  • You can access the text object using the ax.yaxis.get_offset_text().

    import numpy as np
    import matplotlib.pyplot as plt
    
    # Generate some data
    N = 10
    x = np.arange(N)
    y = np.array([i*(10**-38) for i in x])
    
    fig, ax = plt.subplots()
    
    # Plot the data
    ax.plot(x,y)
    
    # Get the text object
    text = ax.yaxis.get_offset_text()
    
    # Set the size.
    text.set_size(30) # Overkill!
    
    plt.show()
    

    I've written the solution above using matplotlib.pyplot rather than pylab though if you absolutely have to use pylab then it can be changed (though I'd recommend you use matplotlib.pyplot in any case as they are pretty much identical you can just do a lot more with pyplot easier).

    Edit

    If you were to use pylab then the code would be:

    pylab.plot(x, y)
    
    ax = pylab.gca() # Gets the current axis object
    
    text = ax.yaxis.get_offset_text() # Get the text object
    
    text.set_size(30) # # Set the size.
    
    pylab.show()
    

    An example plot with an (overkill!) offset text.

    Plot