为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁星coding 2023-07-18 17:42:35
我一直在尝试在颜色栏中绘制一些均匀间隔的刻度线,但到目前为止,我的结果总是给我一个颜色条,刻度线之间的距离与其值成正比,如下图所示:import numpy as npimport matplotlibimport matplotlib as pltT= [0.01, 0.02, 0.03, 0.04] #values for the colourbar to use in equation in for loopx=np.linspace[0, 8, 100]e=1/(np.exp(x)+1)       #factor used in equation dependent on the x-axis valuesa=6.4*10**(-9)         b= 1.51                  # constants for the equationpof6= [number **6 for number in T]norm = matplotlib.colors.Normalize(vmin=np.min(pof6), vmax=np.max(pof6))  #colourbar max and min valuesc_m = matplotlib.cm.cools_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 equationfor 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()我尝试应用网站上建议的一些解决方案,例如将自定义刻度标签均匀地分布在颜色条上,但尚未成功。
查看完整描述

1 回答

?
aluckdog

TA贡献1847条经验 获得超7个赞

您使用的是线性范数,其中pof值彼此非常接近。使用LogNorm会有所帮助。刻度格式化程序可以调整以按其格式显示值**6

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

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()

//img3.sycdn.imooc.com/64b65ecf00015e6609180429.jpg

如果您想要图例,则需要为每条曲线添加标签,例如:


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()


查看完整回答
反对 回复 2023-07-18
  • 1 回答
  • 0 关注
  • 131 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信