![](/img/trans.png)
[英]Matplotlib.axes v matplotlib.pyplot.axes in Eclipse
[英]Matplotlib pyplot axes formatter
我有一張圖片:
這里在y軸我想得到5x10^-5 4x10^-5
等等而不是0.00005 0.00004
。
到目前為止我嘗試過的是:
fig = plt.figure()
ax = fig.add_subplot(111)
y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=True)
ax.yaxis.set_major_formatter(y_formatter)
ax.plot(m_plot,densities1,'-ro',label='0.0<z<0.5')
ax.plot(m_plot,densities2, '-bo',label='0.5<z<1.0')
ax.legend(loc='best',scatterpoints=1)
plt.legend()
plt.show()
這似乎不起作用。 代碼的文檔頁面似乎沒有提供直接的答案。
您可以使用matplotlib.ticker.FuncFormatter
來選擇帶有函數的刻度的格式,如下面的示例代碼所示。 實際上,所有函數都在將輸入(浮點數)轉換為指數表示法,然后將'e'替換為'x10 ^',以便獲得所需的格式。
import matplotlib.pyplot as plt
import matplotlib.ticker as tick
import numpy as np
x = np.linspace(0, 10, 1000)
y = 0.000001*np.sin(10*x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
def y_fmt(x, y):
return '{:2.2e}'.format(x).replace('e', 'x10^')
ax.yaxis.set_major_formatter(tick.FuncFormatter(y_fmt))
plt.show()
如果您願意使用指數表示法(即5.0e-6.0),那么有一個更整潔的解決方案,您可以使用matplotlib.ticker.FormatStrFormatter
選擇格式字符串,如下所示。 字符串格式由標准Python字符串格式規則給出。
...
y_fmt = tick.FormatStrFormatter('%2.2e')
ax.yaxis.set_major_formatter(y_fmt)
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.