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

99封情书 提交于 2021-02-17 02:09:03

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/23392833/python-pylab-how-to-alter-the-size-of-the-label-specifying-the-magnitude-of-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!