失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python画rgb渐变色_请问如何在matplotlib中画出自定义渐变色?

python画rgb渐变色_请问如何在matplotlib中画出自定义渐变色?

时间:2024-05-21 01:49:37

相关推荐

python画rgb渐变色_请问如何在matplotlib中画出自定义渐变色?

算了,自问自答一下,用matplotlib.colors里面的LinearSegmentedColormap类可以自定义color_map。

代码:

import numpy as np

import matplotlib.pyplot as plt

from matplotlib.colors import LinearSegmentedColormap

# Create custom colormaps

cdict = {'red': ((0.0, 1.0, 1.0), # Full red at the first stop

(0.5, 0.0, 0.0), # No red at second stop

(1.0, 1.0, 1.0)), # Full red at final stop

#

'green': ((0.0, 0.0, 0.0), # No green at all stop

(0.5, 0.0, 0.0), #

(1.0, 0.0, 0.0)), #

#

'blue': ((0.0, 0.0, 0.0), # No blue at first stop

(0.5, 1.0, 1.0), # Full blue at second stop

(1.0, 0.0, 0.0))} # No blue at final stop

cmap = LinearSegmentedColormap('Rd_Bl_Rd', cdict, 256)

im = np.outer(np.ones(10), np.linspace(0, 255, 256))

fig = plt.figure(figsize=(9, 2))

ax = fig.add_subplot('111')

ax.set_xticks(np.linspace(0, 255, 3))

ax.set_xticklabels([0, 0.5, 1])

ax.set_yticks([])

ax.set_yticklabels([])

ax.imshow(im, interpolation='nearest', cmap=cmap)

示例图:

如果觉得《python画rgb渐变色_请问如何在matplotlib中画出自定义渐变色?》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。