【问题标题】:How to evenly spread ticks on a colorbar?如何在彩条上均匀分布刻度?
【发布时间】:2020-09-08 05:51:17
【问题描述】:

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

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=,例如 Spread custom tick labels evenly over colorbar,但并未成功。

【问题讨论】:

    标签: python for-loop matplotlib colorbar


    【解决方案1】:

    您使用的是线性范数,其中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()
    

    【讨论】:

      猜你喜欢
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      • 2020-06-12
      • 1970-01-01
      相关资源
      最近更新 更多