如何在颜色条上均匀分布刻度线?

How to evenly spread ticks on a colorbar?

我一直在尝试在我的颜色条中绘制一些均匀间隔的刻度线,但到目前为止,我的结果总是给我一个颜色条,刻度线之间的距离与其值成正比,如下图所示:

import numpy as np
import matplotlib
import matplotlib as plt


T= [0.01, 0.02, 0.03, 0.04] #values for the colourbar to use in equation in for loop
x=np.linspace[0, 8, 100]
e=1/(np.exp(x)+1)       #factor used in equation dependent on the x-axis values
a=6.4*10**(-9)         
b= 1.51                  # constants for the equation

pof6= [number **6 for number in T]

norm = matplotlib.colors.Normalize(vmin=np.min(pof6), vmax=np.max(pof6))  #colourbar max and min values
c_m = matplotlib.cm.cool
s_m = matplotlib.cm.ScalarMappable(cmap='jet', norm=norm)

s_m.set_array([])

#below is the for loop that uses one value of T at a time, represented as t in the equation


for t in pof6:            
    plt.plot(x, b*x/(((a*t*x**2/(m**2))+1)**2)*e, color=s_m.to_rgba(t)) 

func = lambda x,pos: "{:g}".format(x)
fmt = matplotlib.ticker.FuncFormatter(func)

c_bar=plt.colorbar(s_m, format=fmt, ticks=[0.01**6,0.02* 0.03**6, 0.04**6])

plt.legend()
plt.xlabel('y=E/T')
plt.ylabel('$f_{ν_s}$')
c_bar.set_label(r'T(K)')
plt.show()

我曾尝试应用此处 n=在网站上建议的一些解决方案,例如 ,但并未成功。

您使用的是线性范数,其中 pof 值彼此非常接近。它有助于使用 LogNorm。刻度格式器可以调整为以 **6 格式显示值。

下面的代码稍微移动了四个函数,因为示例中的代码似乎所有图都重合了。至少当我使用 m=2 之类的东西时(代码中未定义 m)。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import colors as mcolors
from matplotlib import ticker as mticker

T = [0.01, 0.02, 0.03, 0.04]  # values for the colourbar to use in equation in for loop
x = np.linspace(0, 8, 100)
e = 1 / (np.exp(x) + 1)  # factor used in equation dependent on the x-axis values
a = 6.4 * 10 ** (-9)
b = 1.51  # constants for the equation

pof6 = [number ** 6 for number in T]

norm = mcolors.LogNorm(vmin=np.min(pof6), vmax=np.max(pof6))  # colourbar max and min values
s_m = plt.cm.ScalarMappable(cmap='jet', norm=norm)
s_m.set_array([])

m = 2
for t in pof6:
    plt.plot(x, b * x / (((a * t * x ** 2 / (m ** 2)) + 1) ** 2) * e + 10*t**(1/6), color=s_m.to_rgba(t))

func = lambda x, pos: "{:g}**6".format(x**(1/6) )
fmt = mticker.FuncFormatter(func)

c_bar = plt.colorbar(s_m, format=fmt, ticks=pof6)
c_bar.set_label(r'T(K)')

# plt.legend() # there are no labels set, so a default legend can't be created
plt.xlabel('y=E/T')
plt.ylabel('$f_{ν_s}$')
plt.show()

如果要图例,需要给每条曲线打上标签,例如:

for t in pof6:
    plt.plot(x, b * x / (((a * t * x ** 2 / (m ** 2)) + 1) ** 2) * e, color=s_m.to_rgba(t),
             label=f'$t = {t**(1/6):g}^6$')
plt.legend()