怎么调python界面颜色_如何在Python中创建颜色渐变?

1586010002-jmsa.png

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:

A16dI.jpg

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:

# [, , , , , , , , , ]

Change the inputs to any colors you want

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值