如何在python中创建从绿色到红色的热图?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在python中创建从绿色到红色的热图?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1356字,纯文字阅读大概需要2分钟。
内容图文
![如何在python中创建从绿色到红色的热图?](/upload/InfoBanner/zyjiaocheng/700/7cff9a4a96fe4b1a9e600954a4a9f56f.jpg)
我试图绘制范围为-3到3的对数比率,并希望负比率为绿色,正值为红色,对数比率为0(中心)为白色. matplotlib中没有预先存在的颜色方案提供此选项,我无法弄清楚如何手动输出漂亮的渐变.
解决方法:
您可以使用LinearSegmentedColormap创建自己的.我喜欢在上限和下限将红色和绿色通道设置为小于1.0的值,因此颜色不会太亮(这里我使用的是0.8).根据您的口味调整.
有关详细信息,请参阅matplotlib网站上的custom_cmap example.
这是一个有效的例子:
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
# This dictionary defines the colormap
cdict = {'red': ((0.0, 0.0, 0.0), # no red at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.8, 0.8)), # set to 0.8 so its not too bright at 1
'green': ((0.0, 0.8, 0.8), # set to 0.8 so its not too bright at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.0, 0.0)), # no green at 1
'blue': ((0.0, 0.0, 0.0), # no blue at 0
(0.5, 1.0, 1.0), # all channels set to 1.0 at 0.5 to create white
(1.0, 0.0, 0.0)) # no blue at 1
}
# Create the colormap using the dictionary
GnRd = colors.LinearSegmentedColormap('GnRd', cdict)
# Make a figure and axes
fig,ax = plt.subplots(1)
# Some fake data in the range -3 to 3
dummydata = np.random.rand(5,5)*6.-3.
# Plot the fake data
p=ax.pcolormesh(dummydata,cmap=GnRd,vmin=-3,vmax=3)
# Make a colorbar
fig.colorbar(p,ax=ax)
plt.show()
内容总结
以上是互联网集市为您收集整理的如何在python中创建从绿色到红色的热图?全部内容,希望文章能够帮你解决如何在python中创建从绿色到红色的热图?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。