创建渐变色图-Matplotlib

时间:2018-12-13 01:59:14

标签: python matplotlib colors colormap

我正在尝试创建一个colormapdark red的渐变。我将在下面的屏幕快照中附带输出示例。

enter image description here

我玩过以下light green/white

code

但是我无法复制附件的from matplotlib import pyplot as plt import matplotlib import numpy as np plt.figure() a=np.outer(np.arange(0,1,0.01),np.ones(100)) cdict2 = {'red': [(0.0, 0.1, 0.2), (0.3, 0.4, 0.5), (0.5, 0.5, 0.5), (1.0, 1.0, 1.0)], 'green': [(0.0, 0.0, 0.0), (0.5, 0.5, 0.5), (0.75, 1.0, 1.0), (1.0, 1.0, 1.0)], 'blue': [(0.0, 0.0, 0.0), (0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]} my_cmap2 = matplotlib.colors.LinearSegmentedColormap('my_colormap2',cdict2,256) plt.imshow(a,aspect='auto', cmap =my_cmap2) plt.show() 。我也不确定是否有更有效的方法来实现这一目标?

当前输出:我操纵了值以尝试获取colormap以复制附加的gradient。但是我无法正确理解colormap red-orange-yellow-green

enter image description here

3 个答案:

答案 0 :(得分:1)

从另外this个答案:

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

def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
    new_cmap = colors.LinearSegmentedColormap.from_list(
        'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval),
        cmap(np.linspace(minval, maxval, n)))
    return new_cmap

arr = np.linspace(0, 50, 100).reshape((10, 10))
fig, ax = plt.subplots(ncols = 2)

new_cmap1 = truncate_colormap(plt.get_cmap('jet'), 0.45, 1.0)
new_cmap2 = truncate_colormap(plt.get_cmap('brg'), 1.0, 0.45)

ax[0].imshow(a,aspect='auto', cmap = new_cmap1)
ax[1].imshow(a,aspect='auto', cmap = new_cmap2)
plt.show()

enter image description here

答案 1 :(得分:1)

由于您要尝试模拟现有的渐变色,而不是使用任意颜色创建渐变色,因此找到与测得的渐变值匹配的公式就变得很简单。

首先获取梯度上每个点的平均像素r,g,b值。您首先需要获得纯图像,您发布的图像带有白色边框,边缘有些响动;我使用图像编辑器进行清理。

一旦有了测量值,就可以使用numpy.polyfit进行曲线拟合。我大胆地猜测5度足以满足需要,产生6个系数的数组。在这里,您可以看到覆盖了拟合曲线的测量值图。我想说的很不错。

R,G,B measured values and curve fit

这是使用这些曲线重新创建渐变的代码。

rp = [-1029.86559098,  2344.5778132 , -1033.38786418,  -487.3693808 ,
         298.50245209,   167.25393272]
gp = [  551.32444915, -1098.30287507,   320.71732031,   258.50778539,
         193.11772901,    30.32958789]
bp = [  222.95535971, -1693.48546233,  2455.80348727,  -726.44075478,
         -69.61151887,    67.591787  ]

def clamp(n):
    return min(255, max(0, n))

def gradient(x, rfactors, gfactors, bfactors):
    '''
    Return the r,g,b values along the predefined gradient for
    x in the range [0.0, 1.0].
    '''
    n = len(rfactors)
    r = clamp(int(sum(rfactors[i] * (x**(n-1-i)) for i in range(n))))
    g = clamp(int(sum(gfactors[i] * (x**(n-1-i)) for i in range(n))))
    b = clamp(int(sum(bfactors[i] * (x**(n-1-i)) for i in range(n))))
    return r, g, b

from PIL import Image
im = Image.new('RGB', (742, 30))
ld = im.load()
for x in range(742):
    fx = x / (742 - 1)
    for y in range(30):
        ld[x,y] = gradient(fx, rp, gp, bp)

new gradient

答案 2 :(得分:0)

您可以加载已有的图像并从中创建一个颜色图。不幸的是,图像具有白色边框-首先需要剪切这些边框。

import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import numpy as np

cim = plt.imread("https://i.stack.imgur.com/4q2Ev.png")
cim = cim[cim.shape[0]//2, 8:740, :]

cmap = mcolors.ListedColormap(cim)


data = np.random.rand(10,10)
plt.imshow(data, cmap=cmap)
plt.colorbar()
plt.show()

enter image description here