如何在matplotlib中更改颜色图的强度? [英] How can I change the intensity of a colormap in matplotlib?

查看:55
本文介绍了如何在matplotlib中更改颜色图的强度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

如何更改 'RdBu' 颜色图的强度?例如,如果颜色为(0,0,1),则应将其转换为(0,0,0.8).更普遍,如果颜色是(x,y,z),则应将其转换为(ax,ay,az),其中 a 是标量在零和一之间.

解决方案

这与 Stanley R(现在是 Serenity)的答案非常相似,没有(在我看来)不必要的循环复杂性、附加到列表等:

将 numpy 导入为 np导入matplotlib.pyplot作为plt从 matplotlib.colors 导入 ListedColormapa = 0.5# 获取颜色图颜色,乘以因子a",并创建新的颜色图my_cmap = plt.cm.RdBu(np.arange(plt.cm.RdBu.N))my_cmap[:,0:3] *= amy_cmap = ListedColormap(my_cmap)np.random.seed(1)数据 = np.sort(np.random.rand(8,12))plt.figure()plt.subplot(121)c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap='RdBu', vmin=0.0, vmax=1.0)plt.colorbar(c)plt.subplot(122)c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap=my_cmap, vmin=0.0, vmax=1.0)plt.colorbar(c)plt.show()

I use matplotlib.pyplot.pcolor() to plot a heatmap with matplotlib:

import numpy as np
import matplotlib.pyplot as plt    

np.random.seed(1)
data =  np.sort(np.random.rand(8,12))
plt.figure()
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap='RdBu', vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.show()

How can I change the intensity of the 'RdBu' colormap? E.g., if the color is (0, 0, 1), it should be transformed into (0, 0, 0.8). More generally, if the color is (x, y, z), it should be transformed into (ax, ay, az), where a is some scalar between zero and one.

解决方案

This is quite similar to Stanley R's (edit: now Serenity) answer, without the (in my opinion) unnecessary complexity of loops, appending to lists, et cetera:

import numpy as np
import matplotlib.pyplot as plt    
from matplotlib.colors import ListedColormap

a = 0.5

# Get the colormap colors, multiply them with the factor "a", and create new colormap
my_cmap = plt.cm.RdBu(np.arange(plt.cm.RdBu.N))
my_cmap[:,0:3] *= a 
my_cmap = ListedColormap(my_cmap)

np.random.seed(1)
data =  np.sort(np.random.rand(8,12))
plt.figure()
plt.subplot(121)
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap='RdBu', vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.subplot(122)
c = plt.pcolor(data, edgecolors='k', linewidths=4, cmap=my_cmap, vmin=0.0, vmax=1.0)
plt.colorbar(c)
plt.show()

这篇关于如何在matplotlib中更改颜色图的强度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆