【问题标题】:Colorbar scientific notation, change e^ to 10^Colorbar 科学记数法,将 e^ 改为 10^
【发布时间】:2018-11-16 14:27:56
【问题描述】:

我在二维图中的颜色栏中使用科学记数法。我想写 10^{-3} 而不是 e-3。我试图改变它(见下面的代码)但它不起作用......

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker

x = np.random.rand(100)
y = np.random.rand(100)
z = np.random.rand(100)*0.001

x=x.reshape((10,10))

y=y.reshape((10,10))

z=z.reshape((10,10))

fig, ax = plt.subplots(figsize=(8,6))

cs = ax.contourf(x,y,z, 10)

plt.xticks(fontsize=16,rotation=0)
plt.yticks(fontsize=16,rotation=0)

cbar = plt.colorbar(cs,)
cbar.set_label("test",fontsize = 22)


cbar.formatter.set_scientific(True)
cbar.formatter.set_powerlimits((0, 0))
cbar.ax.tick_params(labelsize=16)
cbar.ax.yaxis.get_offset_text().set_fontsize(22)

cbar.ax.xaxis.major.formatter._useMathText = True

cbar.update_ticks()  

plt.savefig("test.png")

【问题讨论】:

    标签: matplotlib colorbar scientific-notation


    【解决方案1】:

    您似乎想要一个使用 mathtext 的 ScalarFormatter。

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.ticker
    
    x = np.tile(np.arange(10), 10).reshape((10,10))
    y = np.repeat(np.arange(10),10).reshape((10,10))
    z = np.sort(np.random.rand(100)*0.001).reshape((10,10))
    
    fig, ax = plt.subplots(figsize=(8,6))
    cs = ax.contourf(x,y,z, 10)
    
    fmt = matplotlib.ticker.ScalarFormatter(useMathText=True)
    fmt.set_powerlimits((0, 0))
    cbar = plt.colorbar(cs,format=fmt)
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-13
      • 2010-12-29
      相关资源
      最近更新 更多