一种解决方案是设置一个tick formatter,给定指数创建所需形式的标签。
更好的解决方案是在对contourf() 的调用中使用use locator=LogLocator()。使用LogLocator,您可以指定要细分的 10 的倍数。默认subs=(1,):仅精确到 10 的幂。将其更改为 subs=(1,2,) 将使用 10 的幂和 10 的两倍。
import matplotlib.pyplot as plt
from matplotlib.ticker import LogLocator, LogFormatterMathtext
import numpy as np
lons = np.linspace(0, 50, 20)
lats = np.linspace(0, 40, 10)
Ui = np.power(10, np.random.uniform(-2.8, 0.4, size=(10, 20)))
fig, (ax1, ax2, ax3) = plt.subplots(ncols=3, figsize=(16, 5))
kaart1 = ax1.contourf(lons, lats, np.log10(Ui), cmap='BuGn')
cbar1 = plt.colorbar(kaart1, ax=ax1)
cbar1.ax.yaxis.set_major_formatter(lambda x, pos: f'$10^{{{x:.1f}}}$')
ax1.set_title('Special purpose tick formatter')
kaart2 = ax2.contourf(lons, lats, Ui, locator=LogLocator(), cmap='BuGn')
cbar2 = plt.colorbar(kaart2, ax=ax2)
ax2.set_title('Default LogLocator')
kaart3 = ax3.contourf(lons, lats, Ui, locator=LogLocator(subs=(1, 2)), cmap='BuGn')
cbar3 = plt.colorbar(kaart3, ax=ax3)
cbar3.ax.yaxis.set_major_formatter(LogFormatterMathtext())
ax3.set_title('LogLocator(subs=(1, 2))')
plt.tight_layout()
plt.show()