如何在Python中创建颜色渐变

如何在Python中创建颜色渐变

本文介绍了如何在Python中创建颜色渐变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个新的色彩映射,它在绿色和蓝色(或任何其他两种颜色)之间进行内插。我的目标是得到类似的东西:



首先,我真的不知道这是否可以使用蓝色和绿色的线性插值。如果可以,我不知道如何这样做,我发现一些文档关于使用matplotlib方法插值指定的RGB值



真正的麻烦是理解cdict2如何工作。对于示例文档说:



例如:假设您希望红色在下半部分从0增加到1,绿色在中半部分同样增加,然后你会使用:

 从matplotlib import pyplot as plt 
import matplotlib
import numpy as np

plt.figure()
a = np.outer(np.arange(0,1,0.01),np.ones(10))
cdict2 = {'red':[(0.0,0.0,0.0),
(0.5,1.0,1.0),
(1.0,1.0,1.0)],
'green' [(0.0,0.0,0.0),
(0.25,0.0,0.0),
(0.75,1.0,1.0),
(1.0,1.0,1.0)],
'blue':[(0.0,0.0,0.0),
(0.5,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()
pre>

编辑:现在我了解插值如何工作,例如这将给出一个红色到白色插值:



白到红:沿着每个颜色的矩阵列向下,在第一列中,我们有xcoordinate,我们希望插值开始和结束,另外两列是颜色值的实际值那个坐标。

  cdict2 = {'red':[(0.0,1.0,1.0),
(1.0,1.0,1.0 ),
(1.0,1.0,1.0)],
'green':[(0.0,1.0,1.0),
(1.0,0.0,0.0) ,0.0,0.0)],
'blue':[(0.0,1.0,1.0),
(1.0,0.0,0.0),
b $ b

很明显,我想要的梯度将很难通过在RGB空间内插创建...

解决方案

我还没有看到的一个简单的答案是只使用。



通过pip安装

  pip安装颜色

使用如下:

  from color import Color 
red = Color(red)
colors = list(red.range_to(Color(green),10))

#现在是长度10的列表
#包含:
#[< Color red>,< Color#f13600> ;,< Color#e36500>,< Color#d58e00& #c7b000>,< Color#a4b800> ;,< Color#72aa00>,< Color#459c00>,< Color#208e00>,< Color green>]
/ pre>

将输入更改为所需的任何颜色


I want to create a new colormap which interpolates between green and blue (or any other two colours for that matter). My goal is to get something like:

First of all I am really not sure if this can be done using linear interpolation of blue and green. If it can, I'm not sure how to do so, I found some documentation on using a matplotlib method that interpolates specified RGB values here

The real trouble is understanding how "cdict2" works below. For the example the documentation says:

"Example: suppose you want red to increase from 0 to 1 over the bottom half, green to do the same over the middle half, and blue over the top half. Then you would use:"

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(10))
cdict2 = {'red':   [(0.0,  0.0, 0.0),
                   (0.5,  1.0, 1.0),
                   (1.0,  1.0, 1.0)],
         'green': [(0.0,  0.0, 0.0),
                   (0.25, 0.0, 0.0),
                   (0.75, 1.0, 1.0),
                   (1.0,  1.0, 1.0)],
         'blue':  [(0.0,  0.0, 0.0),
                   (0.5,  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()

EDIT: I now understand how the interpolation works, for example this will give a red to white interpolation:

White to red: Going down the columns of the "matrix" for each colour, in column one we have the xcoordinate of where we want the interpolation to start and end and the two other columns are the actual values for the colour value at that coordinate.

cdict2 = {'red':   [(0.0,  1.0, 1.0),
                    (1.0,  1.0, 1.0),
                    (1.0,  1.0, 1.0)],
         'green': [(0.0,  1.0, 1.0),
                   (1.0, 0.0, 0.0),
                   (1.0,  0.0, 0.0)],
     'blue':  [(0.0,  1.0, 1.0),
               (1.0,  0.0, 0.0),
               (1.0,  0.0, 0.0)]}

It is evident that the gradient I want will be very difficult to create by interpolating in RGB space...

解决方案

A simple answer I have not seen yet is to just use the colour package.

Install via pip

pip install colour

Use as so:

from colour import Color
red = Color("red")
colors = list(red.range_to(Color("green"),10))

# colors is now a list of length 10
# Containing:
# [<Color red>, <Color #f13600>, <Color #e36500>, <Color #d58e00>, <Color #c7b000>, <Color #a4b800>, <Color #72aa00>, <Color #459c00>, <Color #208e00>, <Color green>]

Change the inputs to any colors you want

这篇关于如何在Python中创建颜色渐变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:12