擅长:python、mysql、java
<p>您可以使用<a href="http://matplotlib.org/api/ticker_api.html" rel="nofollow noreferrer">^{<cd1>}</a>模块,并将<code>ax.xaxis.set_major_formatter</code>设置为<a href="http://matplotlib.org/api/ticker_api.html#matplotlib.ticker.FuncFormatter" rel="nofollow noreferrer">^{<cd3>}</a>。在</p>
<p>例如:</p>
<pre><code>import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
plt.rcParams['text.usetex'] = True
fig,ax = plt.subplots(1)
x = y = np.arange(0,1.1e4,1e3)
ax.plot(x,y)
def myticks(x,pos):
if x == 0: return "$0$"
exponent = int(np.log10(x))
coeff = x/10**exponent
return r"${:2.0f} \times 10^{{ {:2d} }}$".format(coeff,exponent)
ax.xaxis.set_major_formatter(ticker.FuncFormatter(myticks))
plt.show()
</code></pre>
<p><a href="https://i.stack.imgur.com/HE111.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HE111.png" alt="enter image description here"/></a></p>
<p>注意,这使用<code>LaTeX</code>格式(<code>text.usetex = True</code>)来呈现刻度标签中的指数。还要注意区分<code>LaTeX</code>大括号和python格式字符串大括号所需的双大括号。在</p>